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