001package org.hl7.fhir.utilities;
002
003import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
004import org.apache.commons.lang3.time.FastDateFormat;
005
006import java.util.Calendar;
007import java.util.Date;
008import java.util.TimeZone;
009
010public class DateTimeUtil {
011  private static final FastDateFormat ourHumanDateFormat = FastDateFormat.getDateInstance(FastDateFormat.MEDIUM);
012  private static final FastDateFormat ourHumanDateTimeFormat = FastDateFormat.getDateTimeInstance(FastDateFormat.MEDIUM, FastDateFormat.MEDIUM);
013
014
015  public static String toHumanDisplay(TimeZone theTimeZone, TemporalPrecisionEnum thePrecision, Date theValue, String theValueAsString) {
016    Calendar value = theTimeZone != null ? Calendar.getInstance(theTimeZone) : Calendar.getInstance();
017    value.setTime(theValue);
018
019    switch (thePrecision) {
020      case YEAR:
021      case MONTH:
022      case DAY:
023        return theValueAsString;
024      case MILLI:
025      case SECOND:
026      default:
027        return ourHumanDateTimeFormat.format(value);
028    }
029
030  }
031
032  public static String toHumanDisplayLocalTimezone(TemporalPrecisionEnum thePrecision, Date theValue, String theValueAsString) {
033    switch (thePrecision) {
034      case YEAR:
035      case MONTH:
036      case DAY:
037        return theValueAsString;
038      case MILLI:
039      case SECOND:
040      default:
041        return ourHumanDateTimeFormat.format(theValue);
042    }
043  }
044}