public class TLcdISO8601DateFormat extends Format
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.
format
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));
}
Format.Field
Constructor and Description |
---|
TLcdISO8601DateFormat()
Creates a new
TLcdISO8601DateFormat . |
Modifier and Type | Method and Description |
---|---|
StringBuffer |
format(Object aObject,
StringBuffer toAppendTo,
FieldPosition aPosition)
Formats an object of type
java.util.Date to the ISO 8601
date/time format representation. |
Object |
parseObject(String aSource,
ParsePosition aPosition)
Parses a String encoded in the ISO 8601 date/time format representation
to a
java.util.Date object. |
clone, format, formatToCharacterIterator, parseObject
public TLcdISO8601DateFormat()
TLcdISO8601DateFormat
.public StringBuffer format(Object aObject, StringBuffer toAppendTo, FieldPosition aPosition)
java.util.Date
to the ISO 8601
date/time format representation. The encoding format
is yyyy-MM-dd'T'HH:mm:ss'Z'
expressed in UTC.format
in class Format
aObject
- an object of type java.util.Date
toAppendTo
- where the text is to be appendedaPosition
- not usedtoAppendTo
, with formatted text appendedIllegalArgumentException
- if the object is not of type java.util.Date
public Object parseObject(String aSource, ParsePosition aPosition)
java.util.Date
object. The decoded object format
uses UTC as timezone.parseObject
in class Format
aSource
- the string to be parsedaPosition
- the position to start parsing fromjava.util.Date
object parsed from the String or null
if parsing fails.NullPointerException
- if aSource
or aPosition
is null
.