public class DateUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
DateUtil.DateToISO8601CalendarFactory
Used internally by
DateUtil ; don't use it's implementations for
anything else. |
static class |
DateUtil.TrivialDateToISO8601CalendarFactory
Non-thread-safe factory that hard-references a calendar internally.
|
Modifier and Type | Field and Description |
---|---|
static int |
ACCURACY_HOURS |
static int |
ACCURACY_MILLISECONDS |
static int |
ACCURACY_MINUTES |
static int |
ACCURACY_SECONDS |
static java.util.TimeZone |
UTC |
Modifier and Type | Method and Description |
---|---|
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 date+time with one of the ISO 8601 extended
formats.
|
static java.util.TimeZone |
getTimeZone(java.lang.String name)
Returns the time zone object for the name (or ID).
|
public static final int ACCURACY_HOURS
public static final int ACCURACY_MINUTES
public static final int ACCURACY_SECONDS
public static final int ACCURACY_MILLISECONDS
public static final java.util.TimeZone UTC
public static java.util.TimeZone getTimeZone(java.lang.String name) throws UnrecognizedTimeZoneException
TimeZone.getTimeZone(String)
in that the latest returns GMT
if it doesn't recognize the name, while this throws an
UnrecognizedTimeZoneException
.UnrecognizedTimeZoneException
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)
"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 date/time formats.
This method is thread-safe.date
- the date to convert to ISO 8601 stringdatePart
- whether the date part (year, month, day) will be included
or nottimePart
- whether the time part (hours, minutes, seconds,
milliseconds) will be included or notoffsetPart
- 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 in time 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.)