Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obtain last modification date/time of file as local date/time string

new File(url).lastModified() returns a long equal to the number of milliseconds since the epoch, which is GMT-based.

What is a simple way to convert this to a String representing system-local date/time?

If you really need to see an attempt from me here it is but it's a terrible mess and it's wrong anyway:

LocalDateTime.ofEpochSecond(new File(url).lastModified()/1000,0,ZoneOffset.UTC).atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG))

Beyond LocalDateTime I just have no idea how the time API works.

like image 840
Museful Avatar asked Oct 27 '15 10:10

Museful


People also ask

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.

Which function is used to get the last modification time?

Using getlastmod() Function: The getlastmod() function is used to get the last modification time of the current page.

How to get the last modified time of a file in Java?

The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.

How can I tell when a python file was last modified?

Call os. path. getmtime(path) to find the last modified time of a file at path . The time will be returned as a float giving the number of seconds since the epoch (the platform dependent point at which time starts).


1 Answers

To get the last modified time of a file, you should use Java NIO.2 API, which directly resolves your problem:

FileTime fileTime = Files.getLastModifiedTime(Paths.get(url));
System.out.println(fileTime); // will print date time in "YYYY-MM-DDThh:mm:ss[.s+]Z" format

If you want to access other properties (like last access time, creation time), you can read the basic attributes of a path with Files.readAttributes(path, BasicFileAttributes.class).

like image 81
Tunaki Avatar answered Sep 26 '22 15:09

Tunaki