How to change File Modified Date and Time in Java
As a fun some times we may be thinking of changing file modified date and time from its original modified date and time. We may be thinking of changing date and time and need to puzzle our surrounding. Yes we can achieve this through Java code and lets see how to change it. Lets have fun.
OUTPUT:
BEFORE:
AFTER:
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileModifyDateAndTime {
public static void main(String[] args) {
try{
File file = new File("F:\\testing\\Fool.txt");
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
System.out.println("Original Modified Date and Time : " + sdf.format(file.lastModified()));
//Modifying date and time
Date newDateAndTime = sdf.parse("24/12/1980 12:59:59");
file.setLastModified(newDateAndTime.getTime());
System.out.println("Newly Modified Date and Time : " + sdf.format(file.lastModified()));
}catch(ParseException e){
e.printStackTrace();
}
}
}
OUTPUT:
Original Modified Date and Time : 01/04/2014 19:45:51
Newly Modified Date and Time : 24/12/1980 12:59:59
BEFORE:
AFTER: