Modifying the date of the file is possible through Java using the File class of Java i.e File.setLastModified() method
Java File Class
The File class is Java’s representation of a file or directory pathname. The File class contains several methods for working with the pathname, deleting and renaming files, creating new directories, listing the contents of a directory, and determining several common attributes of files and directories.
The setLastModified() function is a method that comes predefined in the Java File class. The function sets the last modified time of a file or directory. The function sets the last modified value of the file in milliseconds (long type).
Parameters – A string consisting of the new last-modified time in milliseconds.
Return value – It returns a boolean. (True if the operation succeeds, else false).
If the file doesn’t found in the system default date is printed as of 30/01/1970 as the default file is generated by the system.
Now to change the last modified date of a file, follow the given steps.
- First, use the SimpleDateFormat(“mm/dd/yyyy”) constructor to make a new SimpleDateFormat class instance.
- Then, construct a String object with the “mm/dd/yyyy” format.
- Use the parse(String) method of the SimpleDateFormat class to create a new Date object with the date value of the String we created.
- Finally, use File.setLastModified(Date.getTime()) method to set the new “Last Modified” date of our file.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.