public class DateUtil
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
DateUtil.CalendarFieldsToDateConverter
Used internally by
DateUtil ; don't use its implementations for anything else. |
static class |
DateUtil.DateParseException |
static interface |
DateUtil.DateToISO8601CalendarFactory
Used internally by
DateUtil ; don't use its implementations for
anything else. |
static class |
DateUtil.TrivialCalendarFieldsToDateConverter
Non-thread-safe implementation that hard-references a calendar internally.
|
static class |
DateUtil.TrivialDateToISO8601CalendarFactory
Non-thread-safe factory that hard-references a calendar internally.
|
Modifier and Type | Field and Description |
---|---|
static int |
ACCURACY_HOURS
Show hours (24h); always 2 digits, like
00 , 05 , etc. |
static int |
ACCURACY_MILLISECONDS
Show hours, minutes and seconds and up to 3 fraction second digits, without trailing 0-s in the fraction part.
|
static int |
ACCURACY_MILLISECONDS_FORCED
Show hours, minutes and seconds and exactly 3 fraction second digits (even if it's 000)
|
static int |
ACCURACY_MINUTES
Show hours and minutes (even if minutes is 00).
|
static int |
ACCURACY_SECONDS
Show hours, minutes and seconds (even if seconds is 00).
|
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 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).
|
static java.lang.String |
dateToXSString(java.util.Date date,
boolean datePart,
boolean timePart,
boolean offsetPart,
int accuracy,
java.util.TimeZone timeZone,
DateUtil.DateToISO8601CalendarFactory calendarFactory)
Same as
dateToISO8601String(java.util.Date, boolean, boolean, boolean, int, java.util.TimeZone, freemarker.template.utility.DateUtil.DateToISO8601CalendarFactory) , but gives XML Schema compliant format. |
static java.util.TimeZone |
getTimeZone(java.lang.String name)
Returns the time zone object for the name (or ID).
|
static java.util.Date |
parseISO8601Date(java.lang.String dateStr,
java.util.TimeZone defaultTimeZone,
DateUtil.CalendarFieldsToDateConverter calToDateConverter)
Same as
parseXSDate(String, TimeZone, CalendarFieldsToDateConverter) , but for ISO 8601 dates. |
static java.util.Date |
parseISO8601DateTime(java.lang.String dateTimeStr,
java.util.TimeZone defaultTZ,
DateUtil.CalendarFieldsToDateConverter calToDateConverter)
Same as
parseXSDateTime(String, TimeZone, CalendarFieldsToDateConverter) but for ISO 8601 format. |
static java.util.Date |
parseISO8601Time(java.lang.String timeStr,
java.util.TimeZone defaultTZ,
DateUtil.CalendarFieldsToDateConverter calToDateConverter)
Same as
parseXSTime(String, TimeZone, CalendarFieldsToDateConverter) but for ISO 8601 times. |
static java.util.Date |
parseXSDate(java.lang.String dateStr,
java.util.TimeZone defaultTimeZone,
DateUtil.CalendarFieldsToDateConverter calToDateConverter)
Parses an W3C XML Schema date string (not time or date-time).
|
static java.util.Date |
parseXSDateTime(java.lang.String dateTimeStr,
java.util.TimeZone defaultTZ,
DateUtil.CalendarFieldsToDateConverter calToDateConverter)
Parses an W3C XML Schema date-time string (not date or time).
|
static java.util.Date |
parseXSTime(java.lang.String timeStr,
java.util.TimeZone defaultTZ,
DateUtil.CalendarFieldsToDateConverter calToDateConverter)
Parses an W3C XML Schema time string (not date or date-time).
|
static java.util.TimeZone |
parseXSTimeZone(java.lang.String timeZoneStr)
Parses the time zone part from a W3C XML Schema date/time/dateTime.
|
public static final int ACCURACY_HOURS
00
, 05
, etc.public static final int ACCURACY_MINUTES
public static final int ACCURACY_SECONDS
public static final int ACCURACY_MILLISECONDS
public static final int ACCURACY_MILLISECONDS_FORCED
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
- If the time zone name wasn't understoodpublic 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 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.
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 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.)public static java.lang.String dateToXSString(java.util.Date date, boolean datePart, boolean timePart, boolean offsetPart, int accuracy, java.util.TimeZone timeZone, DateUtil.DateToISO8601CalendarFactory calendarFactory)
dateToISO8601String(java.util.Date, boolean, boolean, boolean, int, java.util.TimeZone, freemarker.template.utility.DateUtil.DateToISO8601CalendarFactory)
, but gives XML Schema compliant format.public static java.util.Date parseXSDate(java.lang.String dateStr, java.util.TimeZone defaultTimeZone, DateUtil.CalendarFieldsToDateConverter calToDateConverter) throws DateUtil.DateParseException
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
()
.DateUtil.DateParseException
- if the date is malformed, or if the time
zone offset is unspecified and the defaultTimeZone
is
null
.public static java.util.Date parseISO8601Date(java.lang.String dateStr, java.util.TimeZone defaultTimeZone, DateUtil.CalendarFieldsToDateConverter calToDateConverter) throws DateUtil.DateParseException
parseXSDate(String, TimeZone, CalendarFieldsToDateConverter)
, but for ISO 8601 dates.DateUtil.DateParseException
public static java.util.Date parseXSTime(java.lang.String timeStr, java.util.TimeZone defaultTZ, DateUtil.CalendarFieldsToDateConverter calToDateConverter) throws DateUtil.DateParseException
defaultTZ
paramter will be used.DateUtil.DateParseException
public static java.util.Date parseISO8601Time(java.lang.String timeStr, java.util.TimeZone defaultTZ, DateUtil.CalendarFieldsToDateConverter calToDateConverter) throws DateUtil.DateParseException
parseXSTime(String, TimeZone, CalendarFieldsToDateConverter)
but for ISO 8601 times.DateUtil.DateParseException
public static java.util.Date parseXSDateTime(java.lang.String dateTimeStr, java.util.TimeZone defaultTZ, DateUtil.CalendarFieldsToDateConverter calToDateConverter) throws DateUtil.DateParseException
dateTimeStr
- the string to parse.defaultTZ
- used if the dateTime doesn't specify the
time zone offset explicitly. Can't be null
.DateUtil.DateParseException
- if the dateTime is malformed.public static java.util.Date parseISO8601DateTime(java.lang.String dateTimeStr, java.util.TimeZone defaultTZ, DateUtil.CalendarFieldsToDateConverter calToDateConverter) throws DateUtil.DateParseException
parseXSDateTime(String, TimeZone, CalendarFieldsToDateConverter)
but for ISO 8601 format.DateUtil.DateParseException
public static java.util.TimeZone parseXSTimeZone(java.lang.String timeZoneStr) throws DateUtil.DateParseException
DateUtil.DateParseException
- if the zone is malformed.