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 ca.uhn.fhir.model.api.TemporalPrecisionEnum; 033import ca.uhn.fhir.model.api.annotation.DatatypeDef; 034import org.apache.commons.lang3.Validate; 035 036import java.util.Calendar; 037import java.util.Date; 038import java.util.GregorianCalendar; 039import java.util.TimeZone; 040 041/** 042 * Primitive type "date" in FHIR: any day in a gregorian calendar 043 */ 044 045/** 046 * Represents a FHIR date datatype. Valid precisions values for this type are: 047 * <ul> 048 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 049 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 050 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 051 * </ul> 052 */ 053@DatatypeDef(name = "date") 054public class DateType extends BaseDateTimeType { 055 056 /** 057 * The default precision for this type 058 */ 059 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY; 060 private static final long serialVersionUID = 3L; 061 062 /** 063 * Constructor 064 */ 065 public DateType() { 066 super(); 067 } 068 069 /** 070 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type 071 */ 072 public DateType(Date theDate) { 073 super(theDate, DEFAULT_PRECISION); 074 } 075 076 /** 077 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are: 078 * <ul> 079 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 080 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 081 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 082 * </ul> 083 * 084 * @throws ca.uhn.fhir.parser.DataFormatException If the specified precision is not allowed for this type 085 */ 086 public DateType(Date theDate, TemporalPrecisionEnum thePrecision) { 087 super(theDate, thePrecision); 088 } 089 090 /** 091 * Constructor which accepts a date as a string in FHIR format 092 * 093 * @throws ca.uhn.fhir.parser.DataFormatException If the precision in the date string is not allowed for this type 094 */ 095 public DateType(String theDate) { 096 super(theDate); 097 } 098 099 /** 100 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type. 101 */ 102 public DateType(Calendar theCalendar) { 103 super(theCalendar.getTime(), DEFAULT_PRECISION); 104 setTimeZone(theCalendar.getTimeZone()); 105 } 106 107 /** 108 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type. 109 * <p> 110 * <b>Use caution when using this constructor</b>: The month is 0-indexed but the day is 1-indexed 111 * in order to match the bahaviour of the Java {@link Calendar} type. 112 * </p> 113 * 114 * @param theYear The year, e.g. 2015 115 * @param theMonth The month, e.g. 0 for January 116 * @param theDay The day (1 indexed) e.g. 1 for the first day of the month 117 */ 118 public DateType(int theYear, int theMonth, int theDay) { 119 this(toCalendarZulu(theYear, theMonth, theDay)); 120 } 121 122 @Override 123 public DateType copy() { 124 return new DateType(getValueAsString()); 125 } 126 127 @Override 128 public String fhirType() { 129 return "date"; 130 } 131 132 /** 133 * Returns the default precision for this datatype 134 * 135 * @see #DEFAULT_PRECISION 136 */ 137 @Override 138 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 139 return DEFAULT_PRECISION; 140 } 141 142 @Override 143 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 144 switch (thePrecision) { 145 case YEAR: 146 case MONTH: 147 case DAY: 148 return true; 149 default: 150 return false; 151 } 152 } 153 154 /** 155 * Creates a new instance by parsing an HL7 v3 format date time string 156 */ 157 public static DateType parseV3(String theV3String) { 158 DateType retVal = new DateType(); 159 retVal.setValueAsV3String(theV3String); 160 return retVal; 161 } 162 163 private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) { 164 Validate.isTrue(theMonth >= 0, "theMonth must be between 0 and 11"); 165 Validate.isTrue(theMonth <= 11, "theMonth must be between 0 and 11"); 166 Validate.isTrue(theDay >= 1, "theMonth must be between 0 and 11"); 167 Validate.isTrue(theDay <= 31, "theMonth must be between 0 and 11"); 168 169 GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 170 retVal.set(Calendar.YEAR, theYear); 171 retVal.set(Calendar.MONTH, theMonth); 172 retVal.set(Calendar.DATE, theDay); 173 return retVal; 174 } 175 176 public static InstantType today() { 177 return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault()); 178 } 179}