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 Summary
Fields Modifier and Type Field Description static int
ALT_ZVI
static long
ALT_ZVI_EPOCH
static int
COBOL
static long
COBOL_EPOCH
static String
FILENAME_FORMAT
Human readable timestamp filename stringstatic String
ISO8601_FORMAT
ISO 8601 date output formatter without milliseconds.static String
ISO8601_FORMAT_MS
ISO 8601 date output formatter with milliseconds.static int
MICROSOFT
static long
MICROSOFT_EPOCH
static String
TIMESTAMP_FORMAT
Human readable timestamp stringstatic int
UNIX
Timestamp formats.static long
UNIX_EPOCH
Milliseconds until UNIX epoch.static int
ZVI
static long
ZVI_EPOCH
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
convertDate(long stamp, int format)
Converts the given timestamp into an ISO8601 date.static String
convertDate(long stamp, int format, String outputFormat)
Converts the given timestamp into a date string with the given format.static String
convertDate(long stamp, int format, String outputFormat, boolean correctTimeZoneForGMT)
Converts the given timestamp into a date string with the given format.static String
formatDate(String date, String format)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String[] formats)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String[] formats, boolean lenient)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String[] formats, boolean lenient, String separator)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String[] formats, String separator)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String format, boolean lenient)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String format, boolean lenient, String separator)
Formats the given date as an ISO 8601 date.static String
formatDate(String date, String format, String separator)
Formats the given date as an ISO 8601 date.static String
getFileTimestamp()
static long
getMillisFromTicks(long hi, long lo)
Converts from two-word tick representation to milliseconds.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).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.static String
getTimestamp()
protected static org.joda.time.Instant
parseDate(String date, String format, String separator)
Parse the given date as a Joda instant
-
-
-
Field Detail
-
UNIX
public static final int UNIX
Timestamp formats.- See Also:
- Constant Field Values
-
COBOL
public static final int COBOL
- See Also:
- Constant Field Values
-
MICROSOFT
public static final int MICROSOFT
- See Also:
- Constant Field Values
-
ZVI
public static final int ZVI
- See Also:
- Constant Field Values
-
ALT_ZVI
public static final int ALT_ZVI
- See Also:
- Constant Field Values
-
UNIX_EPOCH
public static final long UNIX_EPOCH
Milliseconds until UNIX epoch.- See Also:
- Constant Field Values
-
COBOL_EPOCH
public static final long COBOL_EPOCH
- See Also:
- Constant Field Values
-
MICROSOFT_EPOCH
public static final long MICROSOFT_EPOCH
- See Also:
- Constant Field Values
-
ZVI_EPOCH
public static final long ZVI_EPOCH
- See Also:
- Constant Field Values
-
ALT_ZVI_EPOCH
public static final long ALT_ZVI_EPOCH
- See Also:
- Constant Field Values
-
ISO8601_FORMAT_MS
public static final String ISO8601_FORMAT_MS
ISO 8601 date output formatter with milliseconds.- See Also:
- Constant Field Values
-
ISO8601_FORMAT
public static final String ISO8601_FORMAT
ISO 8601 date output formatter without milliseconds.- See Also:
- Constant Field Values
-
TIMESTAMP_FORMAT
public static final String TIMESTAMP_FORMAT
Human readable timestamp string- See Also:
- Constant Field Values
-
FILENAME_FORMAT
public static final String FILENAME_FORMAT
Human readable timestamp filename string- See Also:
- Constant Field Values
-
-
Method Detail
-
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 countlo
- 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 millisecondsformat
- the format in whichstamp
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 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 millisecondsformat
- the format in whichstamp
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 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 millisecondsformat
- the format in whichstamp
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 timestampcorrectTimeZoneForGMT
- 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(String date, String format, String separator)
Parse the given date as a Joda instant- Parameters:
date
- The date to parse as a Joda timestampformat
- The date format to parse the string dateseparator
- 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 toformatDate(String, String, boolean)
, with thelenient
flag set to false.- Parameters:
date
- The date to format as ISO 8601format
- 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 toformatDate(String, String, boolean, String)
with thelenient
flag set to false.- Parameters:
date
- The date to format as ISO 8601format
- The date format to parse the string dateseparator
- 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 datelenient
- 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 8601format
- The date format to parse the string datelenient
- 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 toformatDate(String, String[], boolean, String)
withlenient
set to false andseparator
set to null.- Parameters:
date
- The date to format as ISO 8601formats
- 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 toformatDate(String, String[], boolean, String)
withseparator
set to null.- Parameters:
date
- The date to format as ISO 8601.formats
- The date possible formats to parse the string datelenient
- 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 toformatDate(String, String[], boolean, String)
withlenient
set to false.- Parameters:
date
- The date to format as ISO 8601formats
- The date possible formats to parse the string dateseparator
- 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 datelenient
- 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 convertformat
- 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 convertformat
- The date format to parse the string dateseparator
- 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")
-
-