How to Get The Last Modified Date of a File in Java

This post shows how to get the last modified date of a file in Java.

There are more than one options to get the last modified date of a file-

  1. You can use the File.lastModified() method to get the time that the file was last modified.
  2. You can use the Files.getLastModifiedTime() method Java 7 onward.
  3. Java 7 onward you can also use Files.readAttributes() method which returns BasicFileAttributes object which contains many file attributes like creationTime, lastAccessTime, lastModifiedTime. That is also one way to get the last modified date of the file in Java.

Using File.lastModified() method

If you use File.lastModified() method to get the last modified date of the file, method returns 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.
Using SimpleDateFormat you can create the required pattern to convert the returned value to meaningful date and time value.

Output

Using Files.getLastModifiedTime() method

Files.getLastModifiedTime(Path path, LinkOption… options) method available from Java 7 onward is another way to get the last modified date of the file. Here path argument represents the path to the file and options argument indicates how symbolic links are handled in case file is a symbolic link.

Output

Using Files.readAttributes() method to get the file’s last modified date

Files.readAttributes() method returns an instance of BasicFileAttributes which has methods to return various file attributes like creation time, modified date, last access time etc.

Output

That’s all for the topic How to Get The Last Modified Date of a File in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.