Package loci.common

Class DateTools

java.lang.Object
loci.common.DateTools

public final class DateTools
extends java.lang.Object
A utility class with convenience methods for working with dates.
Author:
Curtis Rueden ctrueden at wisc.edu, Chris Allan callan at blackcat.ca, Melissa Linkert melissa at glencoesoftware.com
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static int ALT_ZVI  
    static long ALT_ZVI_EPOCH  
    static int COBOL  
    static long COBOL_EPOCH  
    static java.lang.String FILENAME_FORMAT
    Human readable timestamp filename string
    static java.lang.String ISO8601_FORMAT
    ISO 8601 date output formatter without milliseconds.
    static java.lang.String ISO8601_FORMAT_MS
    ISO 8601 date output formatter with milliseconds.
    static int MICROSOFT  
    static long MICROSOFT_EPOCH  
    static java.lang.String TIMESTAMP_FORMAT
    Human readable timestamp string
    static int UNIX
    Timestamp formats.
    static long UNIX_EPOCH
    Milliseconds until UNIX epoch.
    static int ZVI  
    static long ZVI_EPOCH  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.String convertDate​(long stamp, int format)
    Converts the given timestamp into an ISO8601 date.
    static java.lang.String convertDate​(long stamp, int format, java.lang.String outputFormat)
    Converts the given timestamp into a date string with the given format.
    static java.lang.String convertDate​(long stamp, int format, java.lang.String outputFormat, boolean correctTimeZoneForGMT)
    Converts the given timestamp into a date string with the given format.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String format)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats, boolean lenient)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats, boolean lenient, java.lang.String separator)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats, java.lang.String separator)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String format, boolean lenient)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String format, boolean lenient, java.lang.String separator)
    Formats the given date as an ISO 8601 date.
    static java.lang.String formatDate​(java.lang.String date, java.lang.String format, java.lang.String separator)
    Formats the given date as an ISO 8601 date.
    static java.lang.String getFileTimestamp()  
    static long getMillisFromTicks​(long hi, long lo)
    Converts from two-word tick representation to milliseconds.
    static long getTime​(java.lang.String date, java.lang.String format)
    Converts a string date in the given format to a long timestamp (in Unix format: milliseconds since January 1, 1970).
    static long getTime​(java.lang.String date, java.lang.String format, java.lang.String separator)
    Converts a string date in the given format to a long timestamp (in Unix format: milliseconds since January 1, 1970) with special milliseconds handling.
    static java.lang.String getTimestamp()  
    protected static org.joda.time.Instant parseDate​(java.lang.String date, java.lang.String format, java.lang.String separator)
    Parse the given date as a Joda instant

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • getMillisFromTicks

      public static long getMillisFromTicks​(long hi, long lo)
      Converts from two-word tick representation to milliseconds. Mainly useful in conjunction with COBOL date conversion.
      Parameters:
      hi - the upper 32 bits of the tick count
      lo - the lower 32 bits of the tick count
      Returns:
      the number of milliseconds corresponding to the tick count, where 1 tick = 100 ns
    • convertDate

      public static java.lang.String convertDate​(long stamp, int format)
      Converts the given timestamp into an ISO8601 date.
      Parameters:
      stamp - the format-dependent timestamp in milliseconds
      format - the format in which stamp is stored. This is used to select the epoch value used for normalizing the timestamp to milliseconds since the UNIX epoch. Valid values are #UNIX, #COBOL, #MICROSOFT, #ZVI, and #ALT_ZVI.
      Returns:
      an ISO 8601 formatted timestamp
      See Also:
      UNIX_EPOCH, COBOL_EPOCH, MICROSOFT_EPOCH, ZVI_EPOCH, ALT_ZVI_EPOCH
    • convertDate

      public static java.lang.String convertDate​(long stamp, int format, java.lang.String outputFormat)
      Converts the given timestamp into a date string with the given format.
      Parameters:
      stamp - the format-dependent timestamp in milliseconds
      format - the format in which stamp is stored. This is used to select the epoch value used for normalizing the timestamp to milliseconds since the UNIX epoch. Valid values are #UNIX, #COBOL, #MICROSOFT, #ZVI, and #ALT_ZVI.
      outputFormat - the pattern used for formatting the timestamp
      Returns:
      a timestamp in the specified output format
      See Also:
      UNIX_EPOCH, COBOL_EPOCH, MICROSOFT_EPOCH, ZVI_EPOCH, ALT_ZVI_EPOCH, DateTimeFormat
    • convertDate

      public static java.lang.String convertDate​(long stamp, int format, java.lang.String outputFormat, boolean correctTimeZoneForGMT)
      Converts the given timestamp into a date string with the given format. If correctTimeZoneForGMT is set, then the timestamp will be interpreted as being relative to GMT and not the local time zone.
      Parameters:
      stamp - the format-dependent timestamp in milliseconds
      format - the format in which stamp is stored. This is used to select the epoch value used for normalizing the timestamp to milliseconds since the UNIX epoch. Valid values are #UNIX, #COBOL, #MICROSOFT, #ZVI, and #ALT_ZVI.
      outputFormat - the pattern used for formatting the timestamp
      correctTimeZoneForGMT - true if the timestamp is relative to GMT
      Returns:
      a timestamp in the specified output format
      See Also:
      UNIX_EPOCH, COBOL_EPOCH, MICROSOFT_EPOCH, ZVI_EPOCH, ALT_ZVI_EPOCH, DateTimeFormat
    • parseDate

      protected static org.joda.time.Instant parseDate​(java.lang.String date, java.lang.String format, java.lang.String separator)
      Parse the given date as a Joda instant
      Parameters:
      date - The date to parse as a Joda timestamp
      format - The date format to parse the string date
      separator - The separator for milliseconds
      Returns:
      the Joda Instant object representing the timestamp
      See Also:
      Instant
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String format)
      Formats the given date as an ISO 8601 date. Delegates to formatDate(String, String, boolean), with the lenient flag set to false.
      Parameters:
      date - The date to format as ISO 8601
      format - The date format to parse the string date
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String format, java.lang.String separator)
      Formats the given date as an ISO 8601 date. Delegates to formatDate(String, String, boolean, String) with the lenient flag set to false.
      Parameters:
      date - The date to format as ISO 8601
      format - The date format to parse the string date
      separator - The separator for milliseconds
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String format, boolean lenient)
      Formats the given date as an ISO 8601 date.
      Parameters:
      date - The date to format as ISO 8601.
      format - The date format to parse the string date
      lenient - Whether or not to leniently parse the date.
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String format, boolean lenient, java.lang.String separator)
      Formats the given date as an ISO 8601 date.
      Parameters:
      date - The date to format as ISO 8601
      format - The date format to parse the string date
      lenient - Whether or not to leniently parse the date.
      separator - The separator for milliseconds
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats)
      Formats the given date as an ISO 8601 date. Delegates to formatDate(String, String[], boolean, String) with lenient set to false and separator set to null.
      Parameters:
      date - The date to format as ISO 8601
      formats - The date possible formats to parse the string date
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats, boolean lenient)
      Formats the given date as an ISO 8601 date. Delegates to formatDate(String, String[], boolean, String) with separator set to null.
      Parameters:
      date - The date to format as ISO 8601.
      formats - The date possible formats to parse the string date
      lenient - Whether or not to leniently parse the date.
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats, java.lang.String separator)
      Formats the given date as an ISO 8601 date. Delegates to formatDate(String, String[], boolean, String) with lenient set to false.
      Parameters:
      date - The date to format as ISO 8601
      formats - The date possible formats to parse the string date
      separator - The separator for milliseconds
      Returns:
      an ISO 8601 formatted timestamp
    • formatDate

      public static java.lang.String formatDate​(java.lang.String date, java.lang.String[] formats, boolean lenient, java.lang.String separator)
      Formats the given date as an ISO 8601 date.
      Parameters:
      date - The date to format as ISO 8601.
      formats - The date possible formats to parse the string date
      lenient - Whether or not to leniently parse the date.
      separator - The separator for milliseconds
      Returns:
      an ISO 8601 formatted timestamp
    • getTime

      public static long getTime​(java.lang.String date, java.lang.String format)
      Converts a string date in the given format to a long timestamp (in Unix format: milliseconds since January 1, 1970).
      Parameters:
      date - The date to convert
      format - The date format to parse the string date
      Returns:
      The date in milliseconds
    • getTime

      public static long getTime​(java.lang.String date, java.lang.String format, java.lang.String separator)
      Converts a string date in the given format to a long timestamp (in Unix format: milliseconds since January 1, 1970) with special milliseconds handling.
      Parameters:
      date - The date to convert
      format - The date format to parse the string date
      separator - The separator for milliseconds
      Returns:
      The date in milliseconds
    • getTimestamp

      public static java.lang.String getTimestamp()
      Returns:
      a timestamp for the current timezone in a human-readable locale-independent format ("YYYY-MM-DD HH:MM:SS")
    • getFileTimestamp

      public static java.lang.String getFileTimestamp()
      Returns:
      a timestamp for the current timezone in a format suitable for a filename in a locale-independent format ("YYYY-MM-DD_HH-MM-SS")