Java File Last Modified

Hi guys!!! Welcome to flower brackets blog. In the following example you will learn java file last modified.

java file last modified

Here we will be using file.lastModified() to get the java file last modified. Let’s see the code implementation,

Read Also – Date Time Java Program

Example: java file last modified

import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;

public class LastModifiedDateTime
{
   public static void main(String[] args)
   {
      File file = new File("C:\\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


Using try catch block

Now let’s learn file last modified date java program using try catch block.

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileLastModified
{
   public static final String filepath = "D:\\modifiedFile.txt";
 
   public static void main(String[] args)
   {
      try
      { 
         File file = new File(filepath);
         SimpleDateFormat date = new SimpleDateFormat("MM/dd/yyyy");
         System.out.println("Java file modified (original) : " + date.format(file.lastModified()));
         String newLastModifiedString = "05/10/2003";
         Date modifiedDate = date.parse(newLastModifiedString);
         file.setLastModified(modifiedDate.getTime());
         System.out.println("Java latest last modified (new) : " + date.format(file.lastModified()));
      }
      catch(ParseException pe)
      {
         pe.printStackTrace();
      }
   }
}

Output:

Java file modified (original) : 05/10/2003
Java latest last modified (new) : 06/01/2000


conclusion

That’s it guys. This is all about java get file creation date. I hope you have understood the concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like