Pages Navigation Menu

Coding is much easier than you think

How To Change The File Last Modified Date In Java?

How To Change The File Last Modified Date In Java?

Here’s an example to change the file’s last modified date with the help of File.setLastModified() . This method accept the new modified date in milliseconds (long type), So some data type conversion are required.


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ChangeFileLastModifiedDate {
	public static void main(String[] args) {

		try {

			File file = new File("C:\File.pdf");

			// Print the original last modified date
			SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
			System.out.println("Original Last Modified Date : "
					+ sdf.format(file.lastModified()));

			// Set this date

			String newFileDate = "11/17/2000";

			// Need convert the above date to milliseconds in long value
			Date newDate = sdf.parse(newFileDate);

			// Print the latest last modified date
			System.out.println("Lastest Last Modified Date : "
					+ sdf.format(file.lastModified()));

		} catch (ParseException e) {





Result :

Original Last Modified Date : 12/20/2011
Lastest Last Modified Date : 11/17/2000

About Mohaideen Jamil

I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

%d bloggers like this: