/** * 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; }
private BaseDateTimeDt setTimeZone(String theWholeValue, String theValue) { if (isBlank(theValue)) { throwBadDateFormat(theWholeValue); } else if (theValue.charAt(0) == 'Z') { clearTimeZone(); setTimeZoneZulu(true); } else if (theValue.length() != 6) { throwBadDateFormat(theWholeValue, "Timezone offset must be in the form \"Z\", \"-HH:mm\", or \"+HH:mm\""); } else if (theValue.charAt(3) != ':' || !(theValue.charAt(0) == '+' || theValue.charAt(0) == '-')) { throwBadDateFormat(theWholeValue, "Timezone offset must be in the form \"Z\", \"-HH:mm\", or \"+HH:mm\""); } else { parseInt(theWholeValue, theValue.substring(1, 3), 0, 23); parseInt(theWholeValue, theValue.substring(4, 6), 0, 59); clearTimeZone(); setTimeZone(TimeZone.getTimeZone("GMT" + theValue)); } return this; }
/** * Gets the precision for this datatype (using the default for the given type if not set) * * @see #setPrecision(TemporalPrecisionEnum) */ public TemporalPrecisionEnum getPrecision() { if (myPrecision == null) { return getDefaultPrecisionForDatatype(); } return myPrecision; }
/** * Constructor * * @throws DataFormatException * If the specified precision is not allowed for this type */ public BaseDateTimeDt(String theString) { setValueAsString(theString); validatePrecisionAndThrowDataFormatException(theString, getPrecision()); }
/** * Sets the value for this type using the given Java Date object as the time, and using the default precision for * this datatype (unless the precision is already set), 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. */ @Override public BaseDateTimeDt setValue(Date theValue) { setValue(theValue, getPrecision()); return this; }
private Integer getFieldValue(int theField) { if (getValue() == null) { return null; } Calendar cal = getValueAsCalendar(); return cal.get(theField); }
throwBadDateFormat(value); cal.set(Calendar.YEAR, parseInt(value, value.substring(0, 4), 0, 9999)); precision = TemporalPrecisionEnum.YEAR; if (length > 4) { validateCharAtIndexIs(value, 4, '-'); validateLengthIsAtLeast(value, 7); int monthVal = parseInt(value, value.substring(5, 7), 1, 12) - 1; cal.set(Calendar.MONTH, monthVal); precision = TemporalPrecisionEnum.MONTH; if (length > 7) { validateCharAtIndexIs(value, 7, '-'); validateLengthIsAtLeast(value, 10); cal.set(Calendar.DATE, 1); // for some reason getActualMaximum works incorrectly if date isn't set int actualMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, parseInt(value, value.substring(8, 10), 1, actualMaximum)); precision = TemporalPrecisionEnum.DAY; if (length > 10) { validateLengthIsAtLeast(value, 16); validateCharAtIndexIs(value, 10, 'T'); // yyyy-mm-ddThh:mm:ss int offsetIdx = getOffsetIndex(value); String time; if (offsetIdx == -1) { time = value.substring(11, offsetIdx); String offsetString = value.substring(offsetIdx); setTimeZone(value, offsetString); cal.setTimeZone(getTimeZone());
private void setFieldValue(int theField, int theValue, String theFractionalSeconds, int theMinimum, int theMaximum) { validateValueInRange(theValue, theMinimum, theMaximum); Calendar cal; if (getValue() == null) { cal = new GregorianCalendar(0, 0, 0); } else { cal = getValueAsCalendar(); } if (theField != -1) { cal.set(theField, theValue); } if (theFractionalSeconds != null) { myFractionalSeconds = theFractionalSeconds; } else if (theField == Calendar.MILLISECOND) { myFractionalSeconds = StringUtils.leftPad(Integer.toString(theValue), 3, '0'); } super.setValue(cal.getTime()); }
/** * 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); } }
if (nextObject instanceof BaseDateTimeDt) { BaseDateTimeDt nextValue = (BaseDateTimeDt) nextObject; if (nextValue.isEmpty()) { continue; nextEntity = new ResourceIndexedSearchParamDate(nextSpDef.getName(), nextValue.getValue(), nextValue.getValue(), nextValue.getValueAsString()); } else if (nextObject instanceof PeriodDt) { PeriodDt nextValue = (PeriodDt) nextObject;
/** * Returns a human readable version of this date/time using the system local format, converted to the local timezone * if neccesary. * * @see #toHumanDisplay() for a method which does not convert the time to the local timezone before rendering it. */ public String toHumanDisplayLocalTimezone() { switch (getPrecision()) { case YEAR: case MONTH: case DAY: return ourHumanDateFormat.format(getValue()); case MILLI: case SECOND: default: return ourHumanDateTimeFormat.format(getValue()); } }
/** * Returns the month with 1-index, e.g. 1=the first day of the month */ public Integer getDay() { return getFieldValue(Calendar.DAY_OF_MONTH); }
/** * Returns <code>true</code> if this object represents a date that is today's date * * @throws NullPointerException * if {@link #getValue()} returns <code>null</code> */ public boolean isToday() { Validate.notNull(getValue(), getClass().getSimpleName() + " contains null value"); return DateUtils.isSameDay(new Date(), getValue()); }
/** * 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); }
@Override public void setValueAsString(String theValue) throws DataFormatException { clearTimeZone(); super.setValueAsString(theValue); }
throwBadDateFormat(value); cal.set(Calendar.YEAR, parseInt(value, value.substring(0, 4), 0, 9999)); precision = TemporalPrecisionEnum.YEAR; if (length > 4) { validateCharAtIndexIs(value, 4, '-'); validateLengthIsAtLeast(value, 7); int monthVal = parseInt(value, value.substring(5, 7), 1, 12) - 1; cal.set(Calendar.MONTH, monthVal); precision = TemporalPrecisionEnum.MONTH; if (length > 7) { validateCharAtIndexIs(value, 7, '-'); validateLengthIsAtLeast(value, 10); cal.set(Calendar.DATE, 1); // for some reason getActualMaximum works incorrectly if date isn't set int actualMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH); cal.set(Calendar.DAY_OF_MONTH, parseInt(value, value.substring(8, 10), 1, actualMaximum)); precision = TemporalPrecisionEnum.DAY; if (length > 10) { validateLengthIsAtLeast(value, 16); validateCharAtIndexIs(value, 10, 'T'); // yyyy-mm-ddThh:mm:ss int offsetIdx = getOffsetIndex(value); String time; if (offsetIdx == -1) { time = value.substring(11, offsetIdx); String offsetString = value.substring(offsetIdx); setTimeZone(value, offsetString); cal.setTimeZone(getTimeZone());
private void setFieldValue(int theField, int theValue, String theFractionalSeconds, int theMinimum, int theMaximum) { validateValueInRange(theValue, theMinimum, theMaximum); Calendar cal; if (getValue() == null) { cal = new GregorianCalendar(0, 0, 0); } else { cal = getValueAsCalendar(); } if (theField != -1) { cal.set(theField, theValue); } if (theFractionalSeconds != null) { myFractionalSeconds = theFractionalSeconds; } else if (theField == Calendar.MILLISECOND) { myFractionalSeconds = StringUtils.leftPad(Integer.toString(theValue), 3, '0'); } super.setValue(cal.getTime()); }
/** * 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); } }
/** * Constructor * * @throws DataFormatException * If the specified precision is not allowed for this type */ public BaseDateTimeDt(String theString) { setValueAsString(theString); validatePrecisionAndThrowDataFormatException(theString, getPrecision()); }
private Integer getFieldValue(int theField) { if (getValue() == null) { return null; } Calendar cal = getValueAsCalendar(); return cal.get(theField); }