ca.uhn.fhir.model.primitive.DateTimeDt Maven / Gradle / Ivy
/*
* #%L
* HAPI FHIR - Core Library
* %%
* Copyright (C) 2014 - 2024 Smile CDR, Inc.
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package ca.uhn.fhir.model.primitive;
import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
import ca.uhn.fhir.model.api.annotation.DatatypeDef;
import ca.uhn.fhir.model.api.annotation.SimpleSetter;
import ca.uhn.fhir.parser.DataFormatException;
import java.util.Date;
import java.util.TimeZone;
/**
* Represents a FHIR dateTime datatype. Valid precisions values for this type are:
*
* - {@link TemporalPrecisionEnum#YEAR}
*
- {@link TemporalPrecisionEnum#MONTH}
*
- {@link TemporalPrecisionEnum#DAY}
*
- {@link TemporalPrecisionEnum#SECOND}
*
- {@link TemporalPrecisionEnum#MILLI}
*
*/
@DatatypeDef(name = "dateTime")
public class DateTimeDt extends BaseDateTimeDt {
/**
* The default precision for this type
*/
public static final TemporalPrecisionEnum DEFAULT_PRECISION = TemporalPrecisionEnum.SECOND;
/**
* Constructor
*/
public DateTimeDt() {
super();
}
/**
* Create a new DateTimeDt with seconds precision and the local time zone
*/
@SimpleSetter(suffix = "WithSecondsPrecision")
public DateTimeDt(@SimpleSetter.Parameter(name = "theDate") Date theDate) {
super(theDate, DEFAULT_PRECISION, TimeZone.getDefault());
}
/**
* Constructor which accepts a date value and a precision value. Valid precisions values for this type are:
*
* - {@link TemporalPrecisionEnum#YEAR}
*
- {@link TemporalPrecisionEnum#MONTH}
*
- {@link TemporalPrecisionEnum#DAY}
*
- {@link TemporalPrecisionEnum#SECOND}
*
- {@link TemporalPrecisionEnum#MILLI}
*
*
* @throws DataFormatException
* If the specified precision is not allowed for this type
*/
@SimpleSetter
public DateTimeDt(
@SimpleSetter.Parameter(name = "theDate") Date theDate,
@SimpleSetter.Parameter(name = "thePrecision") TemporalPrecisionEnum thePrecision) {
super(theDate, thePrecision, TimeZone.getDefault());
}
/**
* Create a new instance using a string date/time
*
* @throws DataFormatException
* If the specified precision is not allowed for this type
*/
public DateTimeDt(String theValue) {
super(theValue);
}
/**
* Constructor which accepts a date value, precision value, and time zone. Valid precisions values for this type
* are:
*
* - {@link TemporalPrecisionEnum#YEAR}
*
- {@link TemporalPrecisionEnum#MONTH}
*
- {@link TemporalPrecisionEnum#DAY}
*
- {@link TemporalPrecisionEnum#SECOND}
*
- {@link TemporalPrecisionEnum#MILLI}
*
*/
public DateTimeDt(Date theDate, TemporalPrecisionEnum thePrecision, TimeZone theTimezone) {
super(theDate, thePrecision, theTimezone);
}
@Override
protected boolean isPrecisionAllowed(TemporalPrecisionEnum thePrecision) {
switch (thePrecision) {
case YEAR:
case MONTH:
case DAY:
case SECOND:
case MILLI:
return true;
default:
return false;
}
}
/**
* Returns a new instance of DateTimeDt with the current system time and SECOND precision and the system local time
* zone
*/
public static DateTimeDt withCurrentTime() {
return new DateTimeDt(new Date(), TemporalPrecisionEnum.SECOND, TimeZone.getDefault());
}
/**
* Returns the default precision for this datatype
*
* @see #DEFAULT_PRECISION
*/
@Override
protected TemporalPrecisionEnum getDefaultPrecisionForDatatype() {
return DEFAULT_PRECISION;
}
}