Program to demonstrate how to get and print last modification date of a file on the console in Java.
Output of the program :
package com.hubberspot.code;
import java.io.File;
import java.util.Date;
public class ModifiedDate {
public static void main(String[] args) {
// Creating a new File Object by passing filename to it
File file = new File("customer.txt");
// Getting the last modified date by calling
// lastModified() method of the file object
long modified = file.lastModified();
// Creating a new date object by passing the modified long
// value to its constructor
Date lastModifiedDate = new Date(modified);
// printing the initial modified date to the console
System.out.println("Initial Modification Date : \n");
System.out.println(lastModifiedDate);
System.out.println("\nMaking the main thread sleep for 10 sec ... ");
System.out.println("Till than modifying the file to print new modified date... ");
try {
Thread.sleep(10000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// till main thread is at sleep for 10 sec
// we are modifying the file contents and saving
// the file to print the new modified date
modified = file.lastModified();
lastModifiedDate = new Date(modified);
System.out.println("\nAfter Modification Date : \n");
System.out.println(lastModifiedDate);
}
}
Output of the program :
