Skip to content
Related Articles

Related Articles

java.nio.file.attribute.FileTime Class in Java
  • Last Updated : 09 Mar, 2021

The java.nio.file.attribute.FileTime class is used to get a value representing this file’s timestamp i.e, the time when this file was last modified, accessed, or created.

Class Declaration:

public final class FileTime
extends Object
implements Comparable<FileTime>

Methods:

MethodDescription
compareTo(FileTime other)This method compares the value of two FileTime objects for the order.
equals(Object obj)This method tests this FileTime for equality with the given object.
from(Instant instant)This method returns a FileTime representing the same point of time value on the time-line as the provided Instant object.
from(long value, TimeUnit unit)This method returns a FileTime representing a value at the given unit of granularity.
fromMillis(long value)This method returns a FileTime representing the given value in milliseconds.
hashCode()This method computes a hash code for this file time.
to(TimeUnit unit)This method returns the value at the given unit of granularity.
toInstant()This method converts this FileTime object to an Instant.
toMillis()This method returns the value in milliseconds.
toString()This method returns the string representation of this FileTime.

Below is the implementation of some methods from java.nio.file.attribute.FileTime class in java:

Java




import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
  
public class GFG {
    public static void main(String[] args)
    {
        // Variables for storing time values
        long v1 = 21;
        long v2 = 356;
  
        // Creating an Instant.
        Instant instant
            = Instant.parse("2017-02-03T10:37:30.00Z");
  
        // Creating FileTime object using from
        // Method This method takes an Integer
        // value and a timeunit as parameters.
        FileTime filetime2
            = FileTime.from(v2, TimeUnit.MILLISECONDS);
  
        // Creating FileTime object using fromMillis method.
        FileTime filetime1 = FileTime.fromMillis(v1);
  
        // Creating FileTime object using from Method
        // This method takes an Instant as parameters.
        FileTime filetime3 = FileTime.from(instant);
  
        // Method to convert this filetime to an insatnt.
        Instant instantFromFileTime = filetime3.toInstant();
  
        // Method to convert filetime1 to this timeunit.
        System.out.println(
            filetime1.to(TimeUnit.MILLISECONDS));
  
        // Method to check if filetime1
        // is equal to filetime2 or not.
        if (filetime1.equals(filetime2))
            System.out.println("FileTime are equal");
        else
            System.out.println("FileTime are not equal");
  
        // Method to print hashvalue of filetime1.
        System.out.println(filetime1.hashCode());
  
        // Method to convert filetime1 to milliseconds.
        System.out.println(filetime1.toMillis());
  
        // Method to comapre filetime1 and filetime2.
        System.out.println(filetime1.compareTo(filetime2));
  
        // Method to print string
        // representation of filetime1.
        System.out.println(filetime1.toString());
    }
}
Output
21
FileTime are not equal
1071000000
21
-1
1970-01-01T00:00:00.021Z

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :