How to get last modified date of a file in java

Let’s learn how to get last modified date of a file in java.

How to get last modified date of a file in java

To get last modified date use lastModified() method of class File. Here’s the syntax of lastModified() method.

how to get file last modified date in java

public long lastModified()

lastModified() method returns the time that the file denoted by this abstract pathname was last modified.

The value may be negative indicating the number of milliseconds before the epoch. In the below java program we are using format() method of SimpleDateFormat class to format the output because, value returned from lastModified() method is not readable. Here’s the java program.

import java.util.Date;
import java.text.SimpleDateFormat;
public class LastModifiedDateDemo
   public static void main(String[] args)
      File file = new File("d:\\modifiedFile.txt");
      System.out.println("Before - file last modified date: " + file.lastModified());
      SimpleDateFormat sim = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
      System.out.println("After - file last modified date: " + sim.format(file.lastModified()));


Before – file last modified date: 1486480590836
After – file last modified date: 02/07/2014 05:14:10

Also read – insertion sort in java