java get creation date of file

java get creation date of file

To get the creation date of a file in Java, you can use the File class's lastModified() method. This method returns the date and time that the file was last modified, as the number of milliseconds since the epoch (midnight, January 1, 1970 UTC).

Here's an example of how to use the lastModified() method to get the creation date of a file:

import java.io.File;
import java.util.Date;

public class Main {
  public static void main(String[] args) {
    File file = new File("/path/to/file.txt");
    long timestamp = file.lastModified();
    Date date = new Date(timestamp);
    System.out.println(date); // prints the creation date of the file
  }
}
Sourc‮www:e‬.lautturi.com

In this example, a new File object is created for the file "/path/to/file.txt". The lastModified() method is then called on the File object to get the timestamp of the last modification to the file. A new Date object is then created from the timestamp, and the date is printed to the console.

Please note that the lastModified() method returns 0 if the file does not exist or if an error occurs.

Created Time:2017-11-03 22:21:08  Author:lautturi