Sunday 23 October 2016

How to get the file last modified date in Java?

In Java, File.lastModified() can be used to get the file’s last modified time stamp.

This method will returns the time in milliseconds (long value), we can format it with SimpleDateFormat to make it readable format.

import java.io.File;
import java.text.SimpleDateFormat;

public class FileLastModifiedDate {
     public static void main(String[] args) {

           File file = new File("C:\\HaxLogs.txt");

           System.out.println("Last modified stamp: " + file.lastModified());

           SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

           System.out.println("Formatted date: " + sdf.format(file.lastModified()));
     }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...