View Javadoc
1   package ca.uhn.fhir.model.primitive;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 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  
30  @DatatypeDef(name = "dateTime")
31  public class DateTimeDt extends BaseDateTimeDt {
32  
33  	/**
34  	 * The default precision for this type
35  	 */
36  	public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
37  
38  	/**
39  	 * Constructor
40  	 */
41  	public DateTimeDt() {
42  		super();
43  	}
44  
45  	/**
46  	 * Create a new DateTimeDt
47  	 */
48  	@SimpleSetter(suffix = "WithSecondsPrecision")
49  	public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) {
50  		setValue(theDate);
51  		setPrecision(DEFAULT_PRECISION);
52  		setTimeZone(TimeZone.getDefault());
53  	}
54  
55  	/**
56  	 * Constructor which accepts a date value and a precision value. Valid
57  	 * precisions values for this type are:
58  	 * <ul>
59  	 * <li>{@link TemporalPrecisionEnum#YEAR}
60  	 * <li>{@link TemporalPrecisionEnum#MONTH}
61  	 * <li>{@link TemporalPrecisionEnum#DAY}
62  	 * <li>{@link TemporalPrecisionEnum#SECOND}
63  	 * <li>{@link TemporalPrecisionEnum#MILLI}
64  	 * </ul>
65  	 */
66  	@SimpleSetter
67  	public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate, @SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) {
68  		setValue(theDate);
69  		setPrecision(thePrecision);
70  		setTimeZone(TimeZone.getDefault());
71  	}
72  
73  	/**
74  	 * Create a new instance using a string date/time
75  	 */
76  	public DateTimeDt(String theValue) {
77  		setValueAsString(theValue);
78  	}
79  
80  	@Override
81  	boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
82  		switch (thePrecision) {
83  		case YEAR:
84  		case MONTH:
85  		case DAY:
86  		case SECOND:
87  		case MILLI:
88  			return true;
89  		default:
90  			return false;
91  		}
92  	}
93  
94  	/**
95  	 * Returns a new instance of DateTimeDt with the current system time and SECOND precision
96  	 */
97  	public static DateTimeDt withCurrentTime() {
98  		return new DateTimeDt(new Date(), TemporalPrecisionEnum.SECOND);
99  	}
100 
101 }