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 032/*- 033 * #%L 034 * org.hl7.fhir.r4 035 * %% 036 * Copyright (C) 2014 - 2019 Health Level 7 037 * %% 038 * Licensed under the Apache License, Version 2.0 (the "License"); 039 * you may not use this file except in compliance with the License. 040 * You may obtain a copy of the License at 041 * 042 * http://www.apache.org/licenses/LICENSE-2.0 043 * 044 * Unless required by applicable law or agreed to in writing, software 045 * distributed under the License is distributed on an "AS IS" BASIS, 046 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 047 * See the License for the specific language governing permissions and 048 * limitations under the License. 049 * #L% 050 */ 051 052 053import java.util.Calendar; 054 055/** 056 * Primitive type "date" in FHIR: any day in a gregorian calendar 057 */ 058 059import java.util.Date; 060import java.util.GregorianCalendar; 061import java.util.TimeZone; 062 063import ca.uhn.fhir.model.api.TemporalPrecisionEnum; 064import org.apache.commons.lang3.Validate; 065 066import ca.uhn.fhir.model.api.annotation.DatatypeDef; 067 068/** 069 * Represents a FHIR date datatype. Valid precisions values for this type are: 070 * <ul> 071 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 072 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 073 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 074 * </ul> 075 */ 076@DatatypeDef(name = "date") 077public class DateType extends BaseDateTimeType { 078 079 private static final long serialVersionUID = 3L; 080 081 /** 082 * The default precision for this type 083 */ 084 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY; 085 086 /** 087 * Constructor 088 */ 089 public DateType() { 090 super(); 091 } 092 093 /** 094 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type 095 */ 096 public DateType(Date theDate) { 097 super(theDate, DEFAULT_PRECISION); 098 } 099 100 /** 101 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are: 102 * <ul> 103 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 104 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 105 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 106 * </ul> 107 * 108 * @throws ca.uhn.fhir.parser.DataFormatException 109 * If the specified precision is not allowed for this type 110 */ 111 public DateType(Date theDate, TemporalPrecisionEnum thePrecision) { 112 super(theDate, thePrecision); 113 } 114 115 /** 116 * Constructor which accepts a date as a string in FHIR format 117 * 118 * @throws ca.uhn.fhir.parser.DataFormatException 119 * If the precision in the date string is not allowed for this type 120 */ 121 public DateType(String theDate) { 122 super(theDate); 123 } 124 125 /** 126 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type. 127 */ 128 public DateType(Calendar theCalendar) { 129 super(theCalendar.getTime(), DEFAULT_PRECISION); 130 setTimeZone(theCalendar.getTimeZone()); 131 } 132 133 /** 134 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type. 135 * <p> 136 * <b>Use caution when using this constructor</b>: The month is 0-indexed but the day is 1-indexed 137 * in order to match the bahaviour of the Java {@link Calendar} type. 138 * </p> 139 * 140 * @param theYear The year, e.g. 2015 141 * @param theMonth The month, e.g. 0 for January 142 * @param theDay The day (1 indexed) e.g. 1 for the first day of the month 143 */ 144 public DateType(int theYear, int theMonth, int theDay) { 145 this(toCalendarZulu(theYear, theMonth, theDay)); 146 } 147 148 private static GregorianCalendar toCalendarZulu(int theYear, int theMonth, int theDay) { 149 Validate.isTrue(theMonth >= 0, "theMonth must be between 0 and 11"); 150 Validate.isTrue(theMonth <= 11, "theMonth must be between 0 and 11"); 151 Validate.isTrue(theDay >= 1, "theMonth must be between 0 and 11"); 152 Validate.isTrue(theDay <= 31, "theMonth must be between 0 and 11"); 153 154 GregorianCalendar retVal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); 155 retVal.set(Calendar.YEAR, theYear); 156 retVal.set(Calendar.MONTH, theMonth); 157 retVal.set(Calendar.DATE, theDay); 158 return retVal; 159 } 160 161 @Override 162 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 163 switch (thePrecision) { 164 case YEAR: 165 case MONTH: 166 case DAY: 167 return true; 168 default: 169 return false; 170 } 171 } 172 173 /** 174 * Returns the default precision for this datatype 175 * 176 * @see #DEFAULT_PRECISION 177 */ 178 @Override 179 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 180 return DEFAULT_PRECISION; 181 } 182 183 @Override 184 public DateType copy() { 185 DateType ret = new DateType(getValueAsString()); 186 copyValues(ret); 187 return ret; 188 } 189 190 public static InstantType today() { 191 return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault()); 192 } 193 194 /** 195 * Creates a new instance by parsing an HL7 v3 format date time string 196 */ 197 public static DateType parseV3(String theV3String) { 198 DateType retVal = new DateType(); 199 retVal.setValueAsV3String(theV3String); 200 return retVal; 201 } 202 203 @Override 204 public String fhirType() { 205 return "date"; 206 } 207 208 @Override 209 public boolean isDateTime() { 210 return true; 211 } 212}