Class DateUtil


  • public class DateUtil
    extends java.lang.Object
    Date and time related utilities.
    • Field Detail

      • ACCURACY_HOURS

        public static final int ACCURACY_HOURS
        Show hours (24h); always 2 digits, like 00, 05, etc.
        See Also:
        Constant Field Values
      • ACCURACY_MINUTES

        public static final int ACCURACY_MINUTES
        Show hours and minutes (even if minutes is 00).
        See Also:
        Constant Field Values
      • ACCURACY_SECONDS

        public static final int ACCURACY_SECONDS
        Show hours, minutes and seconds (even if seconds is 00).
        See Also:
        Constant Field Values
      • ACCURACY_MILLISECONDS

        public static final int ACCURACY_MILLISECONDS
        Show hours, minutes and seconds and up to 3 fraction second digits, without trailing 0-s in the fraction part.
        See Also:
        Constant Field Values
      • ACCURACY_MILLISECONDS_FORCED

        public static final int ACCURACY_MILLISECONDS_FORCED
        Show hours, minutes and seconds and exactly 3 fraction second digits (even if it's 000)
        See Also:
        Constant Field Values
      • UTC

        public static final java.util.TimeZone UTC
    • Method Detail

      • dateToISO8601String

        public static java.lang.String dateToISO8601String​(java.util.Date date,
                                                           boolean datePart,
                                                           boolean timePart,
                                                           boolean offsetPart,
                                                           int accuracy,
                                                           java.util.TimeZone timeZone,
                                                           DateUtil.DateToISO8601CalendarFactory calendarFactory)
        Format a date, time or dateTime with one of the ISO 8601 extended formats that is also compatible with the XML Schema format (as far as you don't have dates in the BC era). Examples of possible outputs: "2005-11-27T15:30:00+02:00", "2005-11-27", "15:30:00Z". Note the ":00" in the time zone offset; this is not required by ISO 8601, but included for compatibility with the XML Schema format. Regarding the B.C. issue, those dates will be one year off when read back according the XML Schema format, because of a mismatch between that format and ISO 8601:2000 Second Edition.

        This method is thread-safe.

        Parameters:
        date - the date to convert to ISO 8601 string
        datePart - whether the date part (year, month, day) will be included or not
        timePart - whether the time part (hours, minutes, seconds, milliseconds) will be included or not
        offsetPart - whether the time zone offset part will be included or not. This will be shown as an offset to UTC (examples: "+01", "-02", "+04:30") or as "Z" for UTC (and for UT1 and for GMT+00, since the Java platform doesn't really care about the difference). Note that this can't be true when timePart is false, because ISO 8601 (2004) doesn't mention such patterns.
        accuracy - tells which parts of the date/time to drop. The datePart and timePart parameters are stronger than this. Note that when ACCURACY_MILLISECONDS is specified, the milliseconds part will be displayed as fraction seconds (like "15:30.00.25") with the minimum number of digits needed to show the milliseconds without precision lose. Thus, if the milliseconds happen to be exactly 0, no fraction seconds will be shown at all.
        timeZone - the time zone in which the date/time will be shown. (You may find UTC handy here.) Note that although date-only formats has no time zone offset part, the result still depends on the time zone, as days start and end at different points on the time line in different zones.
        calendarFactory - the factory that will create the calendar used internally for calculations. The point of this parameter is that creating a new calendar is relatively expensive, so it's desirable to reuse calendars and only set their time and zone. (This was tested on Sun JDK 1.6 x86 Win, where it gave 2x-3x speedup.)
      • parseXSDate

        public static java.util.Date parseXSDate​(java.lang.String dateStr,
                                                 java.util.TimeZone defaultTimeZone,
                                                 DateUtil.CalendarFieldsToDateConverter calToDateConverter)
                                          throws DateUtil.DateParseException
        Parses an W3C XML Schema date string (not time or date-time). Unlike in ISO 8601:2000 Second Edition, year -1 means B.C 1, and year 0 is invalid.
        Parameters:
        dateStr - the string to parse.
        defaultTimeZone - used if the date doesn't specify the time zone offset explicitly. Can't be null.
        calToDateConverter - Used internally to calculate the result from the calendar field values. If you don't have a such object around, you can just use new DateUtil.TrivialCalendarFieldsToDateConverter().
        Throws:
        DateUtil.DateParseException - if the date is malformed, or if the time zone offset is unspecified and the defaultTimeZone is null.
      • parseXSDateTime

        public static java.util.Date parseXSDateTime​(java.lang.String dateTimeStr,
                                                     java.util.TimeZone defaultTZ,
                                                     DateUtil.CalendarFieldsToDateConverter calToDateConverter)
                                              throws DateUtil.DateParseException
        Parses an W3C XML Schema date-time string (not date or time). Unlike in ISO 8601:2000 Second Edition, year -1 means B.C 1, and year 0 is invalid.
        Parameters:
        dateTimeStr - the string to parse.
        defaultTZ - used if the dateTime doesn't specify the time zone offset explicitly. Can't be null.
        Throws:
        DateUtil.DateParseException - if the dateTime is malformed.