Last Modified Date Of File In Java

Last Modified Date Of File In Java

Published on July 21 2021

In this tutorial, you will learn how to get the last modified date and time of a file in Java using two different methods. We will also learn how to find last access time, creation time and last modified date details in this tutorial.

Source Code

package theprogrammingportal.javaexamplessourcecode;

import java.io.File;
import java.text.SimpleDateFormat;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;

import java.nio.file.attribute.BasicFileAttributes;

public class LastModifiedDate {

    public static void main(String[] args) {
        String filePath = "C:\\Users\\roshan\\Desktop\\Temp.txt";

        // Method 1 - using java.io package
        File file = new File(filePath);
        System.out.println(file.lastModified());  // in milliseconds and not in readable format
        SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
        System.out.println(sd.format(file.lastModified())); // in proper date format

        // Method 2 - using java.nio package
        try {
            Path path = Paths.get(filePath);
            BasicFileAttributes attribute = Files.readAttributes(path, BasicFileAttributes.class);
            System.out.println(sd.format(attribute.lastModifiedTime().toMillis()));
            System.out.println(sd.format(attribute.lastAccessTime().toMillis()));
            System.out.println(sd.format(attribute.creationTime().toMillis()));

        } catch (Exception e) {
        }

    }
}

Video

 



Next Tutorial

  • Last Modified Date Of File In Java