How to get file last modified date in java

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

how to get file last modified date in java

How to get file last modified date in java

In getting the last modified date of a file in java we use Java.io.File.lastModified() method.

Also read – date time java program

This method return a long value representing the time the file was last modified, measured in milliseconds. You can format it to human readable format using SimpleDateFormat.

Let’s see an example,

import java.io.*;
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 format : " + file.lastModified());
      SimpleDateFormat sim = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
      System.out.println("After file format : " + sim.format(file.lastModified()));
   }
}




Output:

Before file format : 1486480590836
After file format : 02/07/2014 05:14:10