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