Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I touch a folder with current time using set last modified date & time?

Tags:

java

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?

like image 366
Tiffany Avatar asked Dec 19 '18 15:12

Tiffany


People also ask

Which command is used to update modification date for any file?

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.

How do I find the last modified date of a file?

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.

What is the modified date on a file?

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.


2 Answers

This is an example from the documentation, using java.nio.file.Files:

Path path = ...
FileTime now = FileTime.fromMillis(System.currentTimeMillis());
Files.setLastModifiedTime(path, now);
like image 129
Radiodef Avatar answered Sep 21 '22 02:09

Radiodef


I think you should just do folderToTest.setLastModified(System.currentTimeMillis());

like image 40
aBnormaLz Avatar answered Sep 19 '22 02:09

aBnormaLz