How do I get file’s last modification date?

To get file last modification date we can use the lastModified() method of the File class. This method returns a long value. After getting this value you can create an instance of java.util.Date class and pass this value as the parameter. This Date will hold the file’s last modification date.


import java.util.Date;

public class FileLastModificationDate {
    public static void main(String[] args) {
        // Create an instance of file object.
        File file = new File("");
        if (file.exists()) {
            // Get the last modification information.
            Long lastModified = file.lastModified();

            // Create a new date object and pass last modified
            // information to the date object.
            Date date = new Date(lastModified);

            // We know when the last time the file was modified.

Wayan Saryada

Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.