001/* 002Copyright (c) 2011+, HL7, Inc 003All rights reserved. 004 005Redistribution and use in source and binary forms, with or without modification, 006are permitted provided that the following conditions are met: 007 008 * Redistributions of source code must retain the above copyright notice, this 009 list of conditions and the following disclaimer. 010 * Redistributions in binary form must reproduce the above copyright notice, 011 this list of conditions and the following disclaimer in the documentation 012 and/or other materials provided with the distribution. 013 * Neither the name of HL7 nor the names of its contributors may be used to 014 endorse or promote products derived from this software without specific 015 prior written permission. 016 017THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 018ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 019WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 020IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 021INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 022NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 023PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 024WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 025ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 026POSSIBILITY OF SUCH DAMAGE. 027 028*/ 029 030package org.hl7.fhir.r4.model; 031 032import java.util.Calendar; 033 034/** 035 * Primitive type "date" in FHIR: any day in a gregorian calendar 036 */ 037 038import java.util.Date; 039import java.util.GregorianCalendar; 040import java.util.TimeZone; 041 042import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 043import org.apache.commons.lang3.Validate; 044 045import ca.uhn.fhir.model.api.annotation.DatatypeDef; 046 047/** 048 * Represents a FHIR date datatype. Valid precisions values for this type are: 049 * <ul> 050 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 051 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 052 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 053 * </ul> 054 */ 055@DatatypeDef(name = "date") 056public class DateType extends BaseDateTimeType { 057 058 private static final long serialVersionUID = 3L; 059 060 /** 061 * The default precision for this type 062 */ 063 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY; 064 065 /** 066 * Constructor 067 */ 068 public DateType() { 069 super(); 070 } 071 072 /** 073 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type 074 */ 075 public DateType(Date theDate) { 076 super(theDate, DEFAULT_PRECISION); 077 } 078 079 /** 080 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are: 081 * <ul> 082 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 083 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 084 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 085 * </ul> 086 * 087 * @throws ca.uhn.fhir.parser.DataFormatException 088 * If the specified precision is not allowed for this type 089 */ 090 public DateType(Date theDate, TemporalPrecisionEnum thePrecision) { 091 super(theDate, thePrecision); 092 } 093 094 /** 095 * Constructor which accepts a date as a string in FHIR format 096 * 097 * @throws ca.uhn.fhir.parser.DataFormatException 098 * If the precision in the date string is not allowed for this type 099 */ 100 public DateType(String theDate) { 101 super(theDate); 102 } 103 104 /** 105 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type. 106 */ 107 public DateType(Calendar theCalendar) { 108 super(theCalendar.getTime(), DEFAULT_PRECISION); 109 setTimeZone(theCalendar.getTimeZone()); 110 } 111 112 /** 113 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type. 114 * <p> 115 * <b>Use caution when using this constructor</b>: The month is 0-indexed but the day is 1-indexed 116 * in order to match the bahaviour of the Java {@link Calendar} type. 117 * </p> 118 * 119 * @param theYear The year, e.g. 2015 120 * @param theMonth The month, e.g. 0 for January 121 * @param theDay The day (1 indexed) e.g. 1 for the first day of the month 122 */ 123 public DateType(int theYear, int theMonth, int theDay) { 124 this(toCalendarZulu(theYear, theMonth, theDay)); 125 } 126 127 private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) { 128 Validate.isTrue(theMonth >= 0, "theMonth must be between 0 and 11"); 129 Validate.isTrue(theMonth <= 11, "theMonth must be between 0 and 11"); 130 Validate.isTrue(theDay >= 1, "theMonth must be between 0 and 11"); 131 Validate.isTrue(theDay <= 31, "theMonth must be between 0 and 11"); 132 133 GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 134 retVal.set(Calendar.YEAR, theYear); 135 retVal.set(Calendar.MONTH, theMonth); 136 retVal.set(Calendar.DATE, theDay); 137 return retVal; 138 } 139 140 @Override 141 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 142 switch (thePrecision) { 143 case YEAR: 144 case MONTH: 145 case DAY: 146 return true; 147 default: 148 return false; 149 } 150 } 151 152 /** 153 * Returns the default precision for this datatype 154 * 155 * @see #DEFAULT_PRECISION 156 */ 157 @Override 158 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 159 return DEFAULT_PRECISION; 160 } 161 162 @Override 163 public DateType copy() { 164 return new DateType(getValueAsString()); 165 } 166 167 public static InstantType today() { 168 return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault()); 169 } 170 171 /** 172 * Creates a new instance by parsing an HL7 v3 format date time string 173 */ 174 public static DateType parseV3(String theV3String) { 175 DateType retVal = new DateType(); 176 retVal.setValueAsV3String(theV3String); 177 return retVal; 178 } 179 180 @Override 181 public String fhirType() { 182 return "date"; 183 } 184}