Class Iso8601


  • public class Iso8601
    extends Object
    This class provides some utilities for creating and parsing ISO 8601 dates. It dates way back to Java 2 or so and should probably be replaced by java.time classes in introduced in Java 8. It is very widely used in Sec-Lib and other projects.

    Commentary

    This has a few things that need to be mentioned. As per contract
    date to string conversions always only return a date in UTC, i.e., ending with a "Z"
    string to date allows 0 - 3 digits accuracy and various (incl. fractional) time zones which can be notated as +-xxxx or +-xx:xx.

    therefore round-tripping of dates is consistent but not quite straightforward. The actual values of the dates never change but string comparisons might fail. For instance, a date in one timezone will in general not be equal as a formatted string to another in a different timezone, but might be completely identical as date objects.

    Important technical note: There is a "feature" in java's SimpleDateFormat class. In particular, fractions of seconds are handled in an unintiutive way so that errors were being consistently introduced. The method fixSeconds fixes this and there are regression tests to catch this now as well.
    • Field Detail

      • zeros

        protected static String zeros
    • Constructor Detail

      • Iso8601

        public Iso8601()
    • Method Detail

      • date2String

        public static String date2String​(long d)
        Convert a date to a string in the format
        yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        where "Z" represents UTC.
        Parameters:
        d - the date in milliseconds
        Returns:
        the string representation of the date
      • date2String

        public static String date2String​(Date d)
        Convert a date to a string in the format
        yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        where "Z" represents UTC.
        Parameters:
        d - the date
        Returns:
        the string representation of the date
      • date2String

        public static String date2String​(Calendar c)
        Convert a date to a string in the format
        yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
        where "Z" represents UTC.
        Parameters:
        c - the date
        Returns:
        the string representation of the date
      • getUTCDateFormat

        public static DateFormat getUTCDateFormat()
        Provides the date format for UTC used by this utility. Normally it is a good idea to use the built in methods of this class and this is provided for backwards compatibility with earlier releases.
        Returns:
      • getDateFormat

        public static DateFormat getDateFormat()
        Provides the DateFormat used by this utility.
        Returns:
      • fixSeconds

        protected static String fixSeconds​(String rawDate)
                                    throws ParseException
        This normalizes the date so that it always has 3 places for the number of milliseconds. This is unavoidable since there is a serious feature in java's simple date format that will introduce errors.
        Parameters:
        rawDate -
        Returns:
        Throws:
        ParseException
      • string2Date

        public static Calendar string2Date​(String s)
                                    throws ParseException
        Convert a string in the format
        yyyy-MM-dd'T'HH:mm:ss.S[tzd]
        where tzd is either "Z" for UTC or an offset in the format [+-]hh:mm to a date.
        Parameters:
        s -
        Returns:
        Throws:
        ParseException
      • mintCalendar

        protected static Calendar mintCalendar()
        Create a new Calendar
        Returns:
      • main

        public static void main​(String[] args)
        demo of this class
        Parameters:
        args -