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