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

To get last modified date use lastModified() method of class File.

how to get file last modified date in java

lastModified() method return a long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs.

The value may be negative indicating the number of milliseconds before the epoch.

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 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()));
   }
}


Output:

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


Also read – insertion sort in java