Package loci.common

Class DateTools

java.lang.Object
loci.common.DateTools

public final class DateTools extends 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 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 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:
    • convertDate

      public static String convertDate(long stamp, int format, 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:
    • convertDate

      public static String convertDate(long stamp, int format, 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:
    • parseDate

      protected static org.joda.time.Instant parseDate(String date, String format, 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 String formatDate(String date, 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 String formatDate(String date, String format, 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 String formatDate(String date, 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 String formatDate(String date, String format, boolean lenient, 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 String formatDate(String date, 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 String formatDate(String date, 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 String formatDate(String date, String[] formats, 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 String formatDate(String date, String[] formats, boolean lenient, 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(String date, 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(String date, String format, 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 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 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")