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.io.File;
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);
			file.setLastModified(newDate.getTime());

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

		} catch (ParseException e) {

			e.printStackTrace();

		}

	}
}

 

Result :

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

About Mohaideen Jamil