June 24, 2022

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. See example.
  2. You can use the Files.getLastModifiedTime() method Java 7 onward. See example.
  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. See example.

Using File.lastModified() method

If you use File.lastModified() method to get the last modified date of a file, method returns a long value representing the time 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.

public class FileChange {
  public static void main(String[] args) {
    File file = new File("D:\\knpcode\\temp.txt");
    // Pattern for date & time
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    long fileModified = file.lastModified();
    System.out.println("fileModified Value- " + fileModified);
    System.out.println("Date file was last modified- " + sdf.format(fileModified));
  }
}
Output
fileModified Value- 1537430634819
Date file was last modified- 20/09/2018 13:33:54

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 a file in Java. 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.

public class FileChange {
  public static void main(String[] args) {
    Path path = Paths.get("D:\\knpcode\\temp.txt");
    // Pattern for date & time
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    FileTime fileTime;
    try {
      fileTime = Files.getLastModifiedTime(path, LinkOption.NOFOLLOW_LINKS);
      long value = fileTime.toMillis();
      System.out.println("fileModified Value- " + value);
      System.out.println("Date file was last modified- " + sdf.format(value));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
Output
fileModified Value- 1537430634819
Date file was last modified- 20/09/2018 13:33:54

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.

public class FileChange {
	public static void main(String[] args) {
    Path path = Paths.get("F:\\NetJS\\Fromat code.txt");
    // Pattern for date & time
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    BasicFileAttributes fileAttributes;
    try {
      fileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
      long value = fileAttributes.lastAccessTime().toMillis();
      System.out.println("fileModified Value- " + value);
      System.out.println("Date file was last modified- " + sdf.format(value));
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}
Output
fileModified Value- 1537430623307
Date file was last modified- 20/09/2018 13:33:43

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

No comments:

Post a Comment