Top Description Fields Constructors Methods
org.apache.commons.compress.utils

public final Class TimeUtils

extends Object
Class Inheritance
Imports
java.nio.file.attribute.FileTime, java.time.Instant, java.util.Date, java.util.concurrent.TimeUnit, org.apache.commons.io.file.attribute.FileTimes

Utility class for handling time-related types and conversions.

Understanding Unix vs NTFS timestamps:

Since
1.23

Field Summary

Modifier and TypeField and Description
pack-priv static final long
HUNDRED_NANOS_PER_MILLISECOND

The amount of 100-nanosecond intervals in one millisecond.

pack-priv static final long
WINDOWS_EPOCH_OFFSET

Windows File Times

A file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).

Constructor Summary

AccessConstructor and Description
private
TimeUtils()

Private constructor to prevent instantiation of this utility class.

Method Summary

Modifier and TypeMethod and Description
public static boolean

Returns:

true if the time exceeds the minimum or maximum UNIX time, false otherwise.
isUnixTime
(final FileTime
the FileTime to evaluate, can be null.
time
)

Tests whether a FileTime can be safely represented in the standard UNIX time.

public static boolean

Returns:

true if the time can be represented in the standard UNIX time, false otherwise.
isUnixTime
(final long
the number of seconds (since Epoch) to evaluate.
seconds
)

Tests whether a given number of seconds (since Epoch) can be safely represented in the standard UNIX time.

public static Date

Returns:

the Date.
ntfsTimeToDate
(final long
the NTFS time in 100 nanosecond units.
ntfsTime
)

Converts NTFS time (100 nanosecond units since 1 January 1601) to Java time.
public static FileTime

Returns:

the FileTime.
ntfsTimeToFileTime
(final long
the NTFS time in 100-nanosecond units.
ntfsTime
)

Converts NTFS time (100-nanosecond units since 1 January 1601) to a FileTime.
public static Date

Returns:

a Date which corresponds to the supplied time, or null if the time is null.
toDate
(final FileTime
the file time to be converted.
fileTime
)

Deprecated Use FileTimes#toDate(FileTime).
Converts FileTime to a Date.
public static FileTime

Returns:

a FileTime which corresponds to the supplied date, or null if the date is null.
toFileTime
(final Date
the date to be converted.
date
)

Deprecated Use FileTimes#toFileTime(Date).
Converts Date to a FileTime.
public static long

Returns:

the NTFS time.
toNtfsTime
(final Date
the Date.
date
)

Deprecated Use FileTimes#toNtfsTime(Date).
Converts a Date to NTFS time.
public static long

Returns:

the NTFS time in 100-nanosecond units.
toNtfsTime
(final FileTime
the FileTime.
fileTime
)

Converts a FileTime to NTFS time (100-nanosecond units since 1 January 1601).
public static long

Returns:

the NTFS time.
toNtfsTime
(final long
the Java time.
javaTime
)

Converts Java time (milliseconds since Epoch) to NTFS time.

public static long

Returns:

the UNIX timestamp.
toUnixTime
(final FileTime
the original FileTime.
fileTime
)

Converts FileTime to standard UNIX time.

public static FileTime

Returns:

the truncated FileTime.
truncateToHundredNanos
(final FileTime
the FileTime to be truncated.
fileTime
)

Truncates a FileTime to 100-nanosecond precision.

public static FileTime

Returns:

the corresponding FileTime.
unixTimeToFileTime
(final long
UNIX timestamp (in seconds, UTC/GMT).
time
)

Converts standard UNIX time (in seconds, UTC/GMT) to FileTime.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

HUNDRED_NANOS_PER_MILLISECONDback to summary
pack-priv static final long HUNDRED_NANOS_PER_MILLISECOND

The amount of 100-nanosecond intervals in one millisecond.

WINDOWS_EPOCH_OFFSETback to summary
pack-priv static final long WINDOWS_EPOCH_OFFSET

Windows File Times

A file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC). This is the offset of Windows time 0 to Unix epoch in 100-nanosecond intervals.

Constructor Detail

TimeUtilsback to summary
private TimeUtils()

Private constructor to prevent instantiation of this utility class.

Method Detail

isUnixTimeback to summary
public static boolean isUnixTime(final FileTime time)

Tests whether a FileTime can be safely represented in the standard UNIX time.

TODO ? If the FileTime is null, this method always returns true.

Parameters
time:FileTime

the FileTime to evaluate, can be null.

Returns:boolean

true if the time exceeds the minimum or maximum UNIX time, false otherwise.

isUnixTimeback to summary
public static boolean isUnixTime(final long seconds)

Tests whether a given number of seconds (since Epoch) can be safely represented in the standard UNIX time.

Parameters
seconds:long

the number of seconds (since Epoch) to evaluate.

Returns:boolean

true if the time can be represented in the standard UNIX time, false otherwise.

ntfsTimeToDateback to summary
public static Date ntfsTimeToDate(final long ntfsTime)

Converts NTFS time (100 nanosecond units since 1 January 1601) to Java time.

Parameters
ntfsTime:long

the NTFS time in 100 nanosecond units.

Returns:Date

the Date.

Annotations
@Deprecated
ntfsTimeToFileTimeback to summary
public static FileTime ntfsTimeToFileTime(final long ntfsTime)

Converts NTFS time (100-nanosecond units since 1 January 1601) to a FileTime.

Parameters
ntfsTime:long

the NTFS time in 100-nanosecond units.

Returns:FileTime

the FileTime.

Annotations
@Deprecated
See Also
FileTimes#toNtfsTime(FileTime)
toDateback to summary
public static Date toDate(final FileTime fileTime)

Converts FileTime to a Date. If the provided FileTime is null, the returned Date is also null.

Parameters
fileTime:FileTime

the file time to be converted.

Returns:Date

a Date which corresponds to the supplied time, or null if the time is null.

Annotations
@Deprecated
See Also
FileTimes#toFileTime(Date)
toFileTimeback to summary
public static FileTime toFileTime(final Date date)

Converts Date to a FileTime. If the provided Date is null, the returned FileTime is also null.

Parameters
date:Date

the date to be converted.

Returns:FileTime

a FileTime which corresponds to the supplied date, or null if the date is null.

Annotations
@Deprecated
See Also
FileTimes#toDate(FileTime)
toNtfsTimeback to summary
public static long toNtfsTime(final Date date)

Converts a Date to NTFS time.

Parameters
date:Date

the Date.

Returns:long

the NTFS time.

Annotations
@Deprecated
toNtfsTimeback to summary
public static long toNtfsTime(final FileTime fileTime)

Converts a FileTime to NTFS time (100-nanosecond units since 1 January 1601).

Parameters
fileTime:FileTime

the FileTime.

Returns:long

the NTFS time in 100-nanosecond units.

Annotations
@Deprecated
See Also
FileTimes#ntfsTimeToFileTime(long)
toNtfsTimeback to summary
public static long toNtfsTime(final long javaTime)

Converts Java time (milliseconds since Epoch) to NTFS time.

Parameters
javaTime:long

the Java time.

Returns:long

the NTFS time.

toUnixTimeback to summary
public static long toUnixTime(final FileTime fileTime)

Converts FileTime to standard UNIX time.

Parameters
fileTime:FileTime

the original FileTime.

Returns:long

the UNIX timestamp.

truncateToHundredNanosback to summary
public static FileTime truncateToHundredNanos(final FileTime fileTime)

Truncates a FileTime to 100-nanosecond precision.

Parameters
fileTime:FileTime

the FileTime to be truncated.

Returns:FileTime

the truncated FileTime.

unixTimeToFileTimeback to summary
public static FileTime unixTimeToFileTime(final long time)

Converts standard UNIX time (in seconds, UTC/GMT) to FileTime.

Parameters
time:long

UNIX timestamp (in seconds, UTC/GMT).

Returns:FileTime

the corresponding FileTime.