java file creation date

https:/‮www/‬.lautturi.com
java file creation date

In Java, you can use the lastModified() method of the java.io.File class to get the last modification date of a file. The lastModified() method returns the time that the file was last modified, represented as the number of milliseconds since the epoch (midnight, January 1, 1970).

Here's an example of how to use the lastModified() method to get the last modification date of a file in Java:

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

public class Main {
    public static void main(String[] args) {
        String fileName = "path/to/file.txt";
        File file = new File(fileName);
        long lastModified = file.lastModified();
        Date date = new Date(lastModified);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = dateFormat.format(date);
        System.out.println(formattedDate);
    }
}

In this example, a File object is created from the file name, and the lastModified() method is called on the object to get the last modification time. The time is then converted to a Date object and formatted as a string using a SimpleDateFormat object. The formatted date string is then printed to the console.

This code will print the last modification date of the file in the format "yyyy-MM-dd HH:mm:ss". You can use the lastModified() method to get the last modification date of a file in Java.

Created Time:2017-11-03 15:57:13  Author:lautturi