Package com.luciad.text
Class TLcdISO8601DateFormat
java.lang.Object
java.text.Format
com.luciad.text.TLcdISO8601DateFormat
- All Implemented Interfaces:
Serializable,Cloneable
Implementation of
java.text.Format to format and parse java.util.Date objects
to/from the ISO 8601 date/time format representation.
The purpose of the ISO 8601 is to provide an unambiguous and well-defined method of representing dates and times, so as to avoid misinterpretation of numeric representations of dates and times. Dates and times are arranged so the largest temporal term (the year) is placed to the left and each successively smaller term is placed to the right of the previous term.
Parsing
The following example strings can be parsed. They represent increasingly accurate representations of the year 2017, April 4, 2.30pm and 45.11 seconds according to UTC time.- "2017-04"
- "201704"
- "2017-04-05"
- "20170405"
- "2017-04-05T14:30"
- "20170405T1430"
- "2017-04-05T14:30:45"
- "20170405T143045"
- "2017-04-05T14:30:45Z"
- "20170405T143045Z"
- "2017-04-05T14:30:45.11"
- "2017-04-05T14:30:45.11Z"
- "2017-04-05T15:30:45+01:00"
- "2017-04-05T15:30:45.11+01"
- "2017-04-05T15:30:45.11+0100"
- "2017-04-05T15:30:45.11+01:00"
- "2017-04-05T13:30:45.11-01:00"
Formatting
All dates are formatted as UTC date time values accurate to the second, for example "2017-04-05T14:30:45Z". You can customize this by overriding theformat method.
The following example uses DateTimeFormatter.ISO_DATE as alternative ISO format.
public StringBuffer format(Object aObject, StringBuffer toAppendTo, FieldPosition aPosition) {
OffsetDateTime dateTime = OffsetDateTime.ofInstant(((Date) aObject).toInstant(), ZoneId.of("Z"));
return toAppendTo.append(DateTimeFormatter.ISO_DATE.format(dateTime));
}
Thread safety
The formatting and parsing of dates is thread-safe.- Since:
- 2017.0
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from class java.text.Format
Format.Field -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionformat(Object aObject, StringBuffer toAppendTo, FieldPosition aPosition) Formats an object of typejava.util.Dateto the ISO 8601 date/time format representation.parseObject(String aSource, ParsePosition aPosition) Parses a String encoded in the ISO 8601 date/time format representation to ajava.util.Dateobject.Methods inherited from class java.text.Format
clone, format, formatToCharacterIterator, parseObject
-
Constructor Details
-
TLcdISO8601DateFormat
public TLcdISO8601DateFormat()Creates a newTLcdISO8601DateFormat.
-
-
Method Details
-
format
Formats an object of typejava.util.Dateto the ISO 8601 date/time format representation. The encoding format isyyyy-MM-dd'T'HH:mm:ss'Z'expressed in UTC.- Specified by:
formatin classFormat- Parameters:
aObject- an object of typejava.util.DatetoAppendTo- where the text is to be appendedaPosition- not used- Returns:
- the string buffer passed in as
toAppendTo, with formatted text appended - Throws:
IllegalArgumentException- if the object is not of typejava.util.Date
-
parseObject
Parses a String encoded in the ISO 8601 date/time format representation to ajava.util.Dateobject. The decoded object format uses UTC as timezone.- Specified by:
parseObjectin classFormat- Parameters:
aSource- the string to be parsedaPosition- the position to start parsing from- Returns:
- the
java.util.Dateobject parsed from the String ornullif parsing fails. - Throws:
NullPointerException- ifaSourceoraPositionisnull.
-