View Javadoc
1   package ca.uhn.fhir.model.primitive;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.Date;
24  import java.util.TimeZone;
25  
26  import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
27  import ca.uhn.fhir.model.api.annotation.DatatypeDef;
28  import ca.uhn.fhir.model.api.annotation.SimpleSetter;
29  import ca.uhn.fhir.parser.DataFormatException;
30  
31  /**
32   * Represents a FHIR dateTime datatype. Valid precisions values for this type are:
33   * <ul>
34   * <li>{@link TemporalPrecisionEnum#YEAR}
35   * <li>{@link TemporalPrecisionEnum#MONTH}
36   * <li>{@link TemporalPrecisionEnum#DAY}
37   * <li>{@link TemporalPrecisionEnum#SECOND}
38   * <li>{@link TemporalPrecisionEnum#MILLI}
39   * </ul>
40   */
41  @DatatypeDef(name = "dateTime")
42  public class DateTimeDt extends BaseDateTimeDt {
43  
44  	/**
45  	 * The default precision for this type
46  	 */
47  	public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
48  
49  	/**
50  	 * Constructor
51  	 */
52  	public DateTimeDt() {
53  		super();
54  	}
55  
56  	/**
57  	 * Create a new DateTimeDt with seconds precision and the local time zone
58  	 */
59  	@SimpleSetter(suffix = "WithSecondsPrecision")
60  	public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) {
61  		super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
62  	}
63  
64  	/**
65  	 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
66  	 * <ul>
67  	 * <li>{@link TemporalPrecisionEnum#YEAR}
68  	 * <li>{@link TemporalPrecisionEnum#MONTH}
69  	 * <li>{@link TemporalPrecisionEnum#DAY}
70  	 * <li>{@link TemporalPrecisionEnum#SECOND}
71  	 * <li>{@link TemporalPrecisionEnum#MILLI}
72  	 * </ul>
73  	 * 
74  	 * @throws DataFormatException
75  	 *             If the specified precision is not allowed for this type
76  	 */
77  	@SimpleSetter
78  	public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate, @SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) {
79  		super(theDate, thePrecision, TimeZone.getDefault());
80  	}
81  
82  	/**
83  	 * Create a new instance using a string date/time
84  	 * 
85  	 * @throws DataFormatException
86  	 *             If the specified precision is not allowed for this type
87  	 */
88  	public DateTimeDt(String theValue) {
89  		super(theValue);
90  	}
91  
92  	/**
93  	 * Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type
94  	 * are:
95  	 * <ul>
96  	 * <li>{@link TemporalPrecisionEnum#YEAR}
97  	 * <li>{@link TemporalPrecisionEnum#MONTH}
98  	 * <li>{@link TemporalPrecisionEnum#DAY}
99  	 * <li>{@link TemporalPrecisionEnum#SECOND}
100 	 * <li>{@link TemporalPrecisionEnum#MILLI}
101 	 * </ul>
102 	 */
103 	public DateTimeDt(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
104 		super(theDate, thePrecision, theTimezone);
105 	}
106 
107 	@Override
108 	protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
109 		switch (thePrecision) {
110 		case YEAR:
111 		case MONTH:
112 		case DAY:
113 		case SECOND:
114 		case MILLI:
115 			return true;
116 		default:
117 			return false;
118 		}
119 	}
120 
121 	/**
122 	 * Returns a new instance of DateTimeDt with the current system time and SECOND precision and the system local time
123 	 * zone
124 	 */
125 	public static DateTimeDt withCurrentTime() {
126 		return new DateTimeDt(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault());
127 	}
128 
129 	/**
130 	 * Returns the default precision for this datatype
131 	 * 
132 	 * @see #DEFAULT_PRECISION
133 	 */
134 	@Override
135 	protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
136 		return DEFAULT_PRECISION;
137 	}
138 
139 }