I'm trying to update the last modified date of a specific folder, here's what I've got:
public void touchFolder(){
File folderToTest = new File("C:\\Temp");
SimpleDateFormat dateFormatUtc = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
dateFormatUtc.setTimeZone(TimeZone.getTimeZone("UTC"));
String newTime = dateFormatUtc.format(new Date());
folderToTest.setLastModified(Long.parseLong(newTime));
}
I am just putting this code in a test case so don't worry about calling this method etc.
I'm getting errors with the parsing that date format as a long, what's the format used in setting the last modified date & time?
The touch command changes certain dates for each file argument. By default, touch sets both the date of last file modification and the date of last file access to the current time.
The last modifying date of the file can get through Java using the File class of Java i.e File. LastModified() method. The File class is Java's representation of a file or directory pathname.
The modified date of a file or folder represents the last time that file or folder was updated. If you're having trouble with the modified dates of your files or folders, check out these frequently-asked questions.
This is an example from the documentation, using java.nio.file.Files
:
Path path = ...
FileTime now = FileTime.fromMillis(System.currentTimeMillis());
Files.setLastModifiedTime(path, now);
I think you should just do folderToTest.setLastModified(System.currentTimeMillis());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With