Change File Last Modified Date using Java

In this tutorial, I am sharing simple program example about How to Change File Last Modified Date using Java.

ChangeLastModifiedDate.java

package com.dineshkrish.files;
import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* 
* @author Dinesh Krishnan
*
*/
public class ChangeLastModifiedDate {
public static void main(String[] args) {
File file = new File("c://test.doc");// You can change file accordingly
// Creating Calendar Object with Year, Month, Day, Hour, Minutes
Calendar calendar = new GregorianCalendar(2016, Calendar.MAY, 28, 5, 55);
// Get date object using getTime() method
Date date = calendar.getTime();
// Old File Modified Date
Date oldDate = new Date(file.lastModified());
System.out.println("Old Last Modified Date : "+oldDate);
// Setting New Last Modified Date
file.setLastModified(date.getTime());
// New File Modified Date
Date newDate = new Date(file.lastModified());
System.out.println("New Last Modified Date : "+newDate);
}
}

Output

—————–
Old Last Modified Date : Sat May 28 05:55:00 IST 2016
New Last Modified Date : Sat May 28 05:55:00 IST 2016

References

1. Java API Documentation

Tags:

No responses yet

Leave a Reply

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