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.instance.model; 031 032/** 033 * Primitive type "date" in FHIR: any day in a gregorian calendar 034 */ 035 036import java.util.Date; 037import java.util.TimeZone; 038 039import ca.uhn.fhir.model.api.annotation.DatatypeDef; 040 041/** 042 * Represents a FHIR date datatype. Valid precisions values for this type are: 043 * <ul> 044 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 045 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 046 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 047 * </ul> 048 */ 049@DatatypeDef(name = "date") 050public class DateType extends BaseDateTimeType { 051 052 private static final long serialVersionUID = 3L; 053 054 /** 055 * The default precision for this type 056 */ 057 public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.DAY; 058 059 /** 060 * Constructor 061 */ 062 public DateType() { 063 super(); 064 } 065 066 /** 067 * Constructor which accepts a date value and uses the {@link #DEFAULT_PRECISION} for this type 068 */ 069 public DateType(Date theDate) { 070 super(theDate, DEFAULT_PRECISION); 071 } 072 073 /** 074 * Constructor which accepts a date value and a precision value. Valid precisions values for this type are: 075 * <ul> 076 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#YEAR} 077 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#MONTH} 078 * <li>{@link ca.uhn.fhir.model.api.TemporalPrecisionEnum#DAY} 079 * </ul> 080 * 081 * @throws ca.uhn.fhir.parser.DataFormatException 082 * If the specified precision is not allowed for this type 083 */ 084 public DateType(Date theDate, TemporalPrecisionEnum thePrecision) { 085 super(theDate, thePrecision); 086 } 087 088 /** 089 * Constructor which accepts a date as a string in FHIR format 090 * 091 * @throws ca.uhn.fhir.parser.DataFormatException 092 * If the precision in the date string is not allowed for this type 093 */ 094 public DateType(String theDate) { 095 super(theDate); 096 } 097 098 @Override 099 boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) { 100 switch (thePrecision) { 101 case YEAR: 102 case MONTH: 103 case DAY: 104 return true; 105 default: 106 return false; 107 } 108 } 109 110 /** 111 * Returns the default precision for this datatype 112 * 113 * @see #DEFAULT_PRECISION 114 */ 115 @Override 116 protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() { 117 return DEFAULT_PRECISION; 118 } 119 120 @Override 121 public DateType copy() { 122 return new DateType(getValue()); 123 } 124 125 public static InstantType today() { 126 return new InstantType(new Date(), TemporalPrecisionEnum.DAY, TimeZone.getDefault()); 127 } 128 129 /** 130 * Creates a new instance by parsing an HL7 v3 format date time string 131 */ 132 public static DateType parseV3(String theV3String) { 133 DateType retVal = new DateType(); 134 retVal.setValueAsV3String(theV3String); 135 return retVal; 136 } 137 138 public String fhirType() { 139 return "date"; 140 } 141}