java get file creation date modified date code example

Example 1: How to get file last modified date in java

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

Example 2: java get creation date of file

Path file = ...;
BasicFileAttributes attr = Files.readAttributes(file, BasicFileAttributes.class);

System.out.println("creationTime: " + attr.creationTime());
System.out.println("lastAccessTime: " + attr.lastAccessTime());
System.out.println("lastModifiedTime: " + attr.lastModifiedTime());

Tags:

Java Example