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.Calendar; 24 import java.util.Date; 25 import java.util.TimeZone; 26 27 import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 28 import ca.uhn.fhir.model.api.annotation.DatatypeDef; 29 import ca.uhn.fhir.model.api.annotation.SimpleSetter; 30 import ca.uhn.fhir.parser.DataFormatException; 31 32 @DatatypeDef(name = "instant") 33 public class InstantDt extends BaseDateTimeDt { 34 35 /** 36 * The default precision for this type 37 */ 38 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.MILLI; 39 40 /** 41 * Constructor which creates an InstantDt with <b>no timne value</b>. Note 42 * that unlike the default constructor for the Java {@link Date} or 43 * {@link Calendar} objects, this constructor does not initialize the object 44 * with the current time. 45 * 46 * @see #withCurrentTime() to create a new object that has been initialized 47 * with the current time. 48 */ 49 public InstantDt() { 50 super(); 51 } 52 53 /** 54 * Create a new DateTimeDt 55 */ 56 public InstantDt(Calendar theCalendar) { 57 setValue(theCalendar.getTime()); 58 setPrecision(DEFAULT_PRECISION); 59 setTimeZone(theCalendar.getTimeZone()); 60 } 61 62 /** 63 * Create a new DateTimeDt using an existing value. <b>Use this constructor with caution</b>, 64 * as it may create more precision than warranted (since for example it is possible to pass in 65 * a DateTime with only a year, and this constructor will convert to an InstantDt with 66 * milliseconds precision). 67 */ 68 public InstantDt(BaseDateTimeDt theDateTime) { 69 setValue(theDateTime.getValue()); 70 setPrecision(DEFAULT_PRECISION); 71 setTimeZone(theDateTime.getTimeZone()); 72 } 73 74 /** 75 * Create a new DateTimeDt 76 */ 77 @SimpleSetter(suffix = "WithMillisPrecision") 78 public InstantDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) { 79 setValue(theDate); 80 setPrecision(DEFAULT_PRECISION); 81 setTimeZone(TimeZone.getDefault()); 82 } 83 84 /** 85 * Constructor which accepts a date value and a precision value. Valid 86 * precisions values for this type are: 87 * <ul> 88 * <li>{@link TemporalPrecisionEnum#SECOND} 89 * <li>{@link TemporalPrecisionEnum#MILLI} 90 * </ul> 91 */ 92 @SimpleSetter 93 public InstantDt(@SimpleSetter.Parameter(name = "theDate") Date theDate, @SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) { 94 setValue(theDate); 95 setPrecision(thePrecision); 96 setTimeZone(TimeZone.getDefault()); 97 } 98 99 /** 100 * Create a new InstantDt from a string value 101 * 102 * @param theString 103 * The string representation of the string. Must be in a valid 104 * format according to the FHIR specification 105 * @throws DataFormatException 106 */ 107 public InstantDt(String theString) { 108 setValueAsString(theString); 109 } 110 111 /** 112 * Invokes {@link Date#after(Date)} on the contained Date against the given 113 * date 114 * 115 * @throws NullPointerException 116 * If the {@link #getValue() contained Date} is null 117 */ 118 public boolean after(Date theDate) { 119 return getValue().after(theDate); 120 } 121 122 /** 123 * Invokes {@link Date#before(Date)} on the contained Date against the given 124 * date 125 * 126 * @throws NullPointerException 127 * If the {@link #getValue() contained Date} is null 128 */ 129 public boolean before(Date theDate) { 130 return getValue().before(theDate); 131 } 132 133 /** 134 * Sets the value of this instant to the current time (from the system 135 * clock) and the local/default timezone (as retrieved using 136 * {@link TimeZone#getDefault()}. This TimeZone is generally obtained from 137 * the underlying OS. 138 */ 139 public void setToCurrentTimeInLocalTimeZone() { 140 setValue(new Date()); 141 setTimeZone(TimeZone.getDefault()); 142 } 143 144 @Override 145 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 146 switch (thePrecision) { 147 case SECOND: 148 case MILLI: 149 return true; 150 default: 151 return false; 152 } 153 } 154 155 /** 156 * Factory method which creates a new InstantDt with millisecond precision and initializes it with the 157 * current time. 158 */ 159 public static InstantDt withCurrentTime() { 160 return new InstantDt(new Date(), TemporalPrecisionEnum.MILLI); 161 } 162 163 }