/** * Returns the value of this object as a {@link GregorianCalendar} */ public GregorianCalendar getValueAsCalendar() { if (getValue() == null) { return null; } GregorianCalendar cal; if (getTimeZone() != null) { cal = new GregorianCalendar(getTimeZone()); } else { cal = new GregorianCalendar(); } cal.setTime(getValue()); return cal; }
/** * Sets the value for this type using the given Java Date object as the time, and using the specified precision, as * well as the local timezone as determined by the local operating system. Both of * these properties may be modified in subsequent calls if neccesary. * * @param theValue * The date value * @param thePrecision * The precision * @throws DataFormatException */ public void setValue(Date theValue, TemporalPrecisionEnum thePrecision) throws DataFormatException { if (getTimeZone() == null) { setTimeZone(TimeZone.getDefault()); } myPrecision = thePrecision; myFractionalSeconds = ""; if (theValue != null) { long millis = theValue.getTime() % 1000; if (millis < 0) { // This is for times before 1970 (see bug #444) millis = 1000 + millis; } String fractionalSeconds = Integer.toString((int) millis); myFractionalSeconds = StringUtils.leftPad(fractionalSeconds, 3, '0'); } super.setValue(theValue); }
/** * Returns a human readable version of this date/time using the system local format. * <p> * <b>Note on time zones:</b> This method renders the value using the time zone that is contained within the value. * For example, if this date object contains the value "2012-01-05T12:00:00-08:00", * the human display will be rendered as "12:00:00" even if the application is being executed on a system in a * different time zone. If this behaviour is not what you want, use * {@link #toHumanDisplayLocalTimezone()} instead. * </p> */ public String toHumanDisplay() { TimeZone tz = getTimeZone(); Calendar value = tz != null ? Calendar.getInstance(tz) : Calendar.getInstance(); value.setTime(getValue()); switch (getPrecision()) { case YEAR: case MONTH: case DAY: return ourHumanDateFormat.format(value); case MILLI: case SECOND: default: return ourHumanDateTimeFormat.format(value); } }
/** * Create a new DateTimeDt using an existing value. <b>Use this constructor with caution</b>, * as it may create more precision than warranted (since for example it is possible to pass in * a DateTime with only a year, and this constructor will convert to an InstantDt with * milliseconds precision). */ public InstantDt(BaseDateTimeDt theDateTime) { // Do not call super(foo) here, we don't want to trigger a DataFormatException setValue(theDateTime.getValue()); setPrecision(DEFAULT_PRECISION); setTimeZone(theDateTime.getTimeZone()); }
String offsetString = value.substring(offsetIdx); setTimeZone(value, offsetString); cal.setTimeZone(getTimeZone());
/** * Returns the value of this object as a {@link GregorianCalendar} */ public GregorianCalendar getValueAsCalendar() { if (getValue() == null) { return null; } GregorianCalendar cal; if (getTimeZone() != null) { cal = new GregorianCalendar(getTimeZone()); } else { cal = new GregorianCalendar(); } cal.setTime(getValue()); return cal; }
/** * Sets the value for this type using the given Java Date object as the time, and using the specified precision, as * well as the local timezone as determined by the local operating system. Both of * these properties may be modified in subsequent calls if neccesary. * * @param theValue * The date value * @param thePrecision * The precision * @throws DataFormatException */ public void setValue(Date theValue, TemporalPrecisionEnum thePrecision) throws DataFormatException { if (getTimeZone() == null) { setTimeZone(TimeZone.getDefault()); } myPrecision = thePrecision; myFractionalSeconds = ""; if (theValue != null) { long millis = theValue.getTime() % 1000; if (millis < 0) { // This is for times before 1970 (see bug #444) millis = 1000 + millis; } String fractionalSeconds = Integer.toString((int) millis); myFractionalSeconds = StringUtils.leftPad(fractionalSeconds, 3, '0'); } super.setValue(theValue); }
/** * Returns a human readable version of this date/time using the system local format. * <p> * <b>Note on time zones:</b> This method renders the value using the time zone that is contained within the value. * For example, if this date object contains the value "2012-01-05T12:00:00-08:00", * the human display will be rendered as "12:00:00" even if the application is being executed on a system in a * different time zone. If this behaviour is not what you want, use * {@link #toHumanDisplayLocalTimezone()} instead. * </p> */ public String toHumanDisplay() { TimeZone tz = getTimeZone(); Calendar value = tz != null ? Calendar.getInstance(tz) : Calendar.getInstance(); value.setTime(getValue()); switch (getPrecision()) { case YEAR: case MONTH: case DAY: return ourHumanDateFormat.format(value); case MILLI: case SECOND: default: return ourHumanDateTimeFormat.format(value); } }
/** * Create a new DateTimeDt using an existing value. <b>Use this constructor with caution</b>, * as it may create more precision than warranted (since for example it is possible to pass in * a DateTime with only a year, and this constructor will convert to an InstantDt with * milliseconds precision). */ public InstantDt(BaseDateTimeDt theDateTime) { // Do not call super(foo) here, we don't want to trigger a DataFormatException setValue(theDateTime.getValue()); setPrecision(DEFAULT_PRECISION); setTimeZone(theDateTime.getTimeZone()); }
String offsetString = value.substring(offsetIdx); setTimeZone(value, offsetString); cal.setTimeZone(getTimeZone());