Class TLcdISO8601DateFormat

java.lang.Object
java.text.Format
com.luciad.text.TLcdISO8601DateFormat
All Implemented Interfaces:
Serializable, Cloneable

public class TLcdISO8601DateFormat extends Format
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"
If no time zone information is present ("Z" for UTC time, or an offset starting with "+" or "-"), UTC time is assumed.

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 the 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));
 }
 

Thread safety

The formatting and parsing of dates is thread-safe.
Since:
2017.0
See Also:
  • Constructor Details

    • TLcdISO8601DateFormat

      public TLcdISO8601DateFormat()
      Creates a new TLcdISO8601DateFormat.
  • Method Details

    • format

      public StringBuffer format(Object aObject, StringBuffer toAppendTo, FieldPosition aPosition)
      Formats an object of type 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.
      Specified by:
      format in class Format
      Parameters:
      aObject - an object of type java.util.Date
      toAppendTo - where the text is to be appended
      aPosition - not used
      Returns:
      the string buffer passed in as toAppendTo, with formatted text appended
      Throws:
      IllegalArgumentException - if the object is not of type java.util.Date
    • parseObject

      public Object parseObject(String aSource, ParsePosition aPosition)
      Parses a String encoded in the ISO 8601 date/time format representation to a java.util.Date object. The decoded object format uses UTC as timezone.
      Specified by:
      parseObject in class Format
      Parameters:
      aSource - the string to be parsed
      aPosition - the position to start parsing from
      Returns:
      the java.util.Date object parsed from the String or null if parsing fails.
      Throws:
      NullPointerException - if aSource or aPosition is null.