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