BUG-4688: Rework SchemaContext module lookups
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / SimpleDateFormatUtil.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.yangtools.yang.common;
10
11 import java.text.ParseException;
12 import java.text.SimpleDateFormat;
13 import java.util.Date;
14
15 public final class SimpleDateFormatUtil {
16
17     /**
18      * revision format according to Yang spec.
19      */
20     private static final String REVISION_SIMPLE_DATE = "yyyy-MM-dd";
21
22     /**
23      * default Yang date that is used when date is not present.
24      */
25     private static final String DEFAULT_DATE = "1970-01-01";
26
27     /**
28      * {@link SimpleDateFormatUtil#DEFAULT_DATE} for revision statement.
29      */
30     public static final Date DEFAULT_DATE_REV;
31
32     static {
33         final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(REVISION_SIMPLE_DATE);
34
35         try {
36             DEFAULT_DATE_REV = simpleDateFormat.parse(DEFAULT_DATE);
37         } catch (final ParseException e) {
38             throw new ExceptionInInitializerError(e);
39         }
40     }
41
42     private SimpleDateFormatUtil() {
43         throw new UnsupportedOperationException("Utility class should not be instantiated");
44     }
45
46     private static final ThreadLocal<SimpleDateFormat> REVISION_FORMAT = new ThreadLocal<SimpleDateFormat>() {
47
48         @Override
49         protected SimpleDateFormat initialValue() {
50             final SimpleDateFormat fmt = new SimpleDateFormat(REVISION_SIMPLE_DATE);
51             fmt.setLenient(false);
52             return fmt;
53         }
54
55         @Override
56         public void set(final SimpleDateFormat value) {
57             throw new UnsupportedOperationException();
58         }
59
60     };
61
62     public static SimpleDateFormat getRevisionFormat() {
63         return REVISION_FORMAT.get();
64     }
65 }