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

Getting the last modified date of a file in java is one of the common input/output operation.

In getting java file lastmodified to date we use lastModified() method of java.io.File class.

Also read – date time java program

lastModified() 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.

Here’s the java program,

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