001/* 002 * #%L 003 * HAPI FHIR - Core Library 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.model.primitive; 021 022import java.util.Date; 023import java.util.TimeZone; 024 025import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 026import ca.uhn.fhir.model.api.annotation.DatatypeDef; 027import ca.uhn.fhir.model.api.annotation.SimpleSetter; 028import ca.uhn.fhir.parser.DataFormatException; 029 030/** 031 * Represents a FHIR dateTime datatype. Valid precisions values for this type are: 032 * <ul> 033 * <li>{@link TemporalPrecisionEnum#YEAR} 034 * <li>{@link TemporalPrecisionEnum#MONTH} 035 * <li>{@link TemporalPrecisionEnum#DAY} 036 * <li>{@link TemporalPrecisionEnum#SECOND} 037 * <li>{@link TemporalPrecisionEnum#MILLI} 038 * </ul> 039 */ 040@DatatypeDef(name = "dateTime") 041public class DateTimeDt extends BaseDateTimeDt { 042 043 /** 044 * The default precision for this type 045 */ 046 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND; 047 048 /** 049 * Constructor 050 */ 051 public DateTimeDt() { 052 super(); 053 } 054 055 /** 056 * Create a new DateTimeDt with seconds precision and the local time zone 057 */ 058 @SimpleSetter(suffix = "WithSecondsPrecision") 059 public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) { 060 super(theDate, DEFAULT_PRECISION, TimeZone.getDefault()); 061 } 062 063 /** 064 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are: 065 * <ul> 066 * <li>{@link TemporalPrecisionEnum#YEAR} 067 * <li>{@link TemporalPrecisionEnum#MONTH} 068 * <li>{@link TemporalPrecisionEnum#DAY} 069 * <li>{@link TemporalPrecisionEnum#SECOND} 070 * <li>{@link TemporalPrecisionEnum#MILLI} 071 * </ul> 072 * 073 * @throws DataFormatException 074 * If the specified precision is not allowed for this type 075 */ 076 @SimpleSetter 077 public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate, @SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) { 078 super(theDate, thePrecision, TimeZone.getDefault()); 079 } 080 081 /** 082 * Create a new instance using a string date/time 083 * 084 * @throws DataFormatException 085 * If the specified precision is not allowed for this type 086 */ 087 public DateTimeDt(String theValue) { 088 super(theValue); 089 } 090 091 /** 092 * Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type 093 * are: 094 * <ul> 095 * <li>{@link TemporalPrecisionEnum#YEAR} 096 * <li>{@link TemporalPrecisionEnum#MONTH} 097 * <li>{@link TemporalPrecisionEnum#DAY} 098 * <li>{@link TemporalPrecisionEnum#SECOND} 099 * <li>{@link TemporalPrecisionEnum#MILLI} 100 * </ul> 101 */ 102 public DateTimeDt(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) { 103 super(theDate, thePrecision, theTimezone); 104 } 105 106 @Override 107 protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 108 switch (thePrecision) { 109 case YEAR: 110 case MONTH: 111 case DAY: 112 case SECOND: 113 case MILLI: 114 return true; 115 default: 116 return false; 117 } 118 } 119 120 /** 121 * Returns a new instance of DateTimeDt with the current system time and SECOND precision and the system local time 122 * zone 123 */ 124 public static DateTimeDt withCurrentTime() { 125 return new DateTimeDt(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault()); 126 } 127 128 /** 129 * Returns the default precision for this datatype 130 * 131 * @see #DEFAULT_PRECISION 132 */ 133 @Override 134 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 135 return DEFAULT_PRECISION; 136 } 137 138}