
001package org.hl7.fhir.r5.model; 002 003/* 004 Copyright (c) 2011+, HL7, Inc. 005 All rights reserved. 006 007 Redistribution and use in source and binary forms, with or without modification, 008 are permitted provided that the following conditions are met: 009 010 * Redistributions of source code must retain the above copyright notice, this 011 list of conditions and the following disclaimer. 012 * Redistributions in binary form must reproduce the above copyright notice, 013 this list of conditions and the following disclaimer in the documentation 014 and/or other materials provided with the distribution. 015 * Neither the name of HL7 nor the names of its contributors may be used to 016 endorse or promote products derived from this software without specific 017 prior written permission. 018 019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 022 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 024 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 025 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 026 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 028 POSSIBILITY OF SUCH DAMAGE. 029 030 */ 031 032 033 034import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 035import ca.uhn.fhir.model.api.annotation.DatatypeDef; 036import org.apache.commons.lang3.Validate; 037 038import java.util.Calendar; 039import java.util.Date; 040import java.util.GregorianCalendar; 041import java.util.TimeZone; 042 043/** 044 * Primitive type "date" in FHIR: any day in a gregorian calendar 045 */ 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, "theDay must be between 1 and 31"); 131 Validate.isTrue(theDay <= 31, "theDay must be between 1 and 31"); 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 DateType ret = new DateType(getValueAsString()); 165 copyValues(ret); 166 return ret; 167 } 168 169 public static InstantType today() { 170 return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault()); 171 } 172 173 /** 174 * Creates a new instance by parsing an HL7 v3 format date time string 175 */ 176 public static DateType parseV3(String theV3String) { 177 DateType retVal = new DateType(); 178 retVal.setValueAsV3String(theV3String); 179 return retVal; 180 } 181 182 @Override 183 public String fhirType() { 184 return "date"; 185 } 186 187 @Override 188 public boolean isDateTime() { 189 return true; 190 } 191}