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