Class FileTimes
- java.lang.Object
-
- org.apache.commons.io.file.attribute.FileTimes
-
public final class FileTimes extends java.lang.Object
Helps useFileTimeand interoperate Date and NTFS times.An NTFS 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.
- Since:
- 2.12.0
- See Also:
- NTFS File Times
-
-
Field Summary
Fields Modifier and Type Field Description static java.nio.file.attribute.FileTimeEPOCHConstant for the1970-01-01T00:00:00Zepochas a time stamp attribute.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.nio.file.attribute.FileTimefromUnixTime(long time)Converts standard Unix time (in seconds, UTC/GMT) toFileTime.static booleanisUnixTime(long seconds)Tests whether a given number of seconds (since Epoch) can be safely represented in the standard Unix time.static booleanisUnixTime(java.nio.file.attribute.FileTime time)Tests whether a FileTime can be safely represented in the standard Unix time.static java.nio.file.attribute.FileTimeminusMillis(java.nio.file.attribute.FileTime fileTime, long millisToSubtract)Subtracts milliseconds from a source FileTime.static java.nio.file.attribute.FileTimeminusNanos(java.nio.file.attribute.FileTime fileTime, long nanosToSubtract)Subtracts nanoseconds from a source FileTime.static java.nio.file.attribute.FileTimeminusSeconds(java.nio.file.attribute.FileTime fileTime, long secondsToSubtract)Subtracts seconds from a source FileTime.static java.nio.file.attribute.FileTimenow()Obtains the current instant FileTime from the system clock.static java.util.DatentfsTimeToDate(long ntfsTime)Converts an NTFS time (100 nanosecond units since 1 January 1601) to aDate.static java.nio.file.attribute.FileTimentfsTimeToFileTime(long ntfsTime)Converts an NTFS time (100-nanosecond units since 1 January 1601) to aFileTime.static java.nio.file.attribute.FileTimeplusMillis(java.nio.file.attribute.FileTime fileTime, long millisToAdd)Adds milliseconds to aFileTime.static java.nio.file.attribute.FileTimeplusNanos(java.nio.file.attribute.FileTime fileTime, long nanosToSubtract)Adds nanoseconds to aFileTime.static java.nio.file.attribute.FileTimeplusSeconds(java.nio.file.attribute.FileTime fileTime, long secondsToAdd)Adds seconds to aFileTime.static voidsetLastModifiedTime(java.nio.file.Path path)Sets the last modified time of the given file path to now.static java.util.DatetoDate(java.nio.file.attribute.FileTime fileTime)Converts aFileTimeto aDate.static java.nio.file.attribute.FileTimetoFileTime(java.util.Date date)Converts aDateto aFileTime.static longtoNtfsTime(long javaTime)Converts a Java time (milliseconds since Epoch) to NTFS time.static longtoNtfsTime(java.nio.file.attribute.FileTime fileTime)Converts aFileTimeto NTFS time (100-nanosecond units since 1 January 1601).static longtoNtfsTime(java.util.Date date)Converts aDateto NTFS time.static longtoUnixTime(java.nio.file.attribute.FileTime fileTime)Converts aFileTimeto standard Unix time in seconds.
-
-
-
Field Detail
-
EPOCH
public static final java.nio.file.attribute.FileTime EPOCH
Constant for the1970-01-01T00:00:00Zepochas a time stamp attribute.- See Also:
Instant.EPOCH
-
-
Method Detail
-
fromUnixTime
public static java.nio.file.attribute.FileTime fromUnixTime(long time)
Converts standard Unix time (in seconds, UTC/GMT) toFileTime.- Parameters:
time- Unix timestamp (seconds).- Returns:
- the corresponding FileTime.
- Since:
- 2.16.0
-
isUnixTime
public static boolean isUnixTime(java.nio.file.attribute.FileTime time)
Tests whether a FileTime can be safely represented in the standard Unix time.If the FileTime is null, this method returns true.
- Parameters:
time- the FileTime to evaluate, can be null.- Returns:
- true if the time exceeds the minimum or maximum Unix time, false otherwise.
- Since:
- 2.16.0
-
isUnixTime
public static boolean isUnixTime(long seconds)
Tests whether a given number of seconds (since Epoch) can be safely represented in the standard Unix time.- Parameters:
seconds- the number of seconds (since Epoch) to evaluate.- Returns:
- true if the time can be represented in the standard Unix time, false otherwise.
- Since:
- 2.16.0
-
minusMillis
public static java.nio.file.attribute.FileTime minusMillis(java.nio.file.attribute.FileTime fileTime, long millisToSubtract)
Subtracts milliseconds from a source FileTime.- Parameters:
fileTime- The source FileTime.millisToSubtract- The milliseconds to subtract.- Returns:
- The resulting FileTime.
-
minusNanos
public static java.nio.file.attribute.FileTime minusNanos(java.nio.file.attribute.FileTime fileTime, long nanosToSubtract)
Subtracts nanoseconds from a source FileTime.- Parameters:
fileTime- The source FileTime.nanosToSubtract- The nanoseconds to subtract.- Returns:
- The resulting FileTime.
-
minusSeconds
public static java.nio.file.attribute.FileTime minusSeconds(java.nio.file.attribute.FileTime fileTime, long secondsToSubtract)
Subtracts seconds from a source FileTime.- Parameters:
fileTime- The source FileTime.secondsToSubtract- The seconds to subtract.- Returns:
- The resulting FileTime.
-
now
public static java.nio.file.attribute.FileTime now()
Obtains the current instant FileTime from the system clock.- Returns:
- the current instant FileTime from the system clock.
-
ntfsTimeToDate
public static java.util.Date ntfsTimeToDate(long ntfsTime)
Converts an NTFS time (100 nanosecond units since 1 January 1601) to aDate.An NTFS file time is a 64-bit value for the number of 100-nanosecond intervals since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
- Parameters:
ntfsTime- the NTFS time, 100-nanosecond units since 1 January 1601.- Returns:
- the Date input.
- See Also:
- NTFS File Times
-
ntfsTimeToFileTime
public static java.nio.file.attribute.FileTime ntfsTimeToFileTime(long ntfsTime)
Converts an NTFS time (100-nanosecond units since 1 January 1601) to aFileTime.An NTFS file time is a 64-bit value for the number of 100-nanosecond intervals since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
- Parameters:
ntfsTime- the NTFS time, 100-nanosecond units since 1 January 1601.- Returns:
- the FileTime input.
- See Also:
toNtfsTime(FileTime), NTFS File Times
-
plusMillis
public static java.nio.file.attribute.FileTime plusMillis(java.nio.file.attribute.FileTime fileTime, long millisToAdd)
Adds milliseconds to aFileTime.- Parameters:
fileTime- The source FileTime.millisToAdd- The milliseconds to add.- Returns:
- The resulting FileTime.
-
plusNanos
public static java.nio.file.attribute.FileTime plusNanos(java.nio.file.attribute.FileTime fileTime, long nanosToSubtract)
Adds nanoseconds to aFileTime.- Parameters:
fileTime- The source FileTime.nanosToSubtract- The nanoseconds to subtract.- Returns:
- The resulting FileTime.
-
plusSeconds
public static java.nio.file.attribute.FileTime plusSeconds(java.nio.file.attribute.FileTime fileTime, long secondsToAdd)
Adds seconds to aFileTime.- Parameters:
fileTime- The source FileTime.secondsToAdd- The seconds to add.- Returns:
- The resulting FileTime.
-
setLastModifiedTime
public static void setLastModifiedTime(java.nio.file.Path path) throws java.io.IOException
Sets the last modified time of the given file path to now.- Parameters:
path- The file path to set.- Throws:
java.io.IOException- if an I/O error occurs.
-
toDate
public static java.util.Date toDate(java.nio.file.attribute.FileTime fileTime)
Converts aFileTimeto aDate. If the provided FileTime isnull, the returned Date is alsonull.- Parameters:
fileTime- the file time to be converted.- Returns:
- a
Datewhich corresponds to the supplied time, ornullif the time isnull. - See Also:
toFileTime(Date)
-
toFileTime
public static java.nio.file.attribute.FileTime toFileTime(java.util.Date date)
Converts aDateto aFileTime. If the provided Date isnull, the returned FileTime is alsonull.- Parameters:
date- the date to be converted.- Returns:
- a
FileTimewhich corresponds to the supplied date, ornullif the date isnull. - See Also:
toDate(FileTime)
-
toNtfsTime
public static long toNtfsTime(java.util.Date date)
Converts aDateto NTFS time.An NTFS file time is a 64-bit value for the number of 100-nanosecond intervals since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
- Parameters:
date- the Date input.- Returns:
- the NTFS time, 100-nanosecond units since 1 January 1601.
-
toNtfsTime
public static long toNtfsTime(java.nio.file.attribute.FileTime fileTime)
Converts aFileTimeto NTFS time (100-nanosecond units since 1 January 1601).An NTFS file time is a 64-bit value for the number of 100-nanosecond intervals since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
- Parameters:
fileTime- the FileTime input.- Returns:
- the NTFS time, 100-nanosecond units since 1 January 1601.
-
toNtfsTime
public static long toNtfsTime(long javaTime)
Converts a Java time (milliseconds since Epoch) to NTFS time.An NTFS file time is a 64-bit value for the number of 100-nanosecond intervals since 12:00 A.M. January 1, 1601 Coordinated Universal Time (UTC).
- Parameters:
javaTime- the Java time- Returns:
- the NTFS time, 100-nanosecond units since 1 January 1601.
- Since:
- 2.16.0
-
toUnixTime
public static long toUnixTime(java.nio.file.attribute.FileTime fileTime)
Converts aFileTimeto standard Unix time in seconds.The returned seconds value may lie out of bounds of Unix time. Check with
isUnixTime(long).- Parameters:
fileTime- the original FileTime.- Returns:
- the Unix timestamp or 0 if the input is null.
- Since:
- 2.16.0
- See Also:
isUnixTime(long)
-
-