Skip to content

Instantly share code, notes, and snippets.

@Sam-Kruglov
Created July 2, 2018 13:07
Show Gist options
  • Save Sam-Kruglov/317f2c7ecee1e74c0456254c605563a3 to your computer and use it in GitHub Desktop.
Save Sam-Kruglov/317f2c7ecee1e74c0456254c605563a3 to your computer and use it in GitHub Desktop.
Check reading the last modified time from a file
class FileTimeChecker{
public static void main(String[] args) throws IOException, URISyntaxException {
Path path = Files.write(Paths.get("/hey.txt"), "hey".getBytes());
File file = new File("/hey.txt");
Instant fileInstant = Instant.ofEpochMilli(file.lastModified());
System.out.println("fileInstant: " + fileInstant);
Instant nioInstant = Files.getLastModifiedTime(path).toInstant();
System.out.println("nioInstant: " + nioInstant);
ZonedDateTime fileTimeUtc = fileInstant.atZone(ZoneId.of("UTC"));
System.out.println("fileTimeUtc: " + fileTimeUtc);
ZonedDateTime nioTimeUtc = nioInstant.atZone(ZoneId.of("UTC"));
System.out.println("nioTimeUtc: " + nioTimeUtc);
ZonedDateTime fileTimeSystem = fileInstant.atZone(ZoneId.systemDefault());
System.out.println("fileTimeSystem: " + fileTimeSystem);
ZonedDateTime nioTimeSystem = nioInstant.atZone(ZoneId.systemDefault());
System.out.println("nioTimeSystem: " + nioTimeSystem);
}
}
@Sam-Kruglov
Copy link
Author

Sam-Kruglov commented Jul 2, 2018

Produced the following output:

fileInstant:     2018-07-02T13:04:03.945Z
nioInstant:      2018-07-02T13:04:03.945726Z
fileTimeUtc:     2018-07-02T13:04:03.945Z[UTC]
nioTimeUtc:      2018-07-02T13:04:03.945726Z[UTC]
fileTimeSystem:  2018-07-02T16:04:03.945+03:00[Europe/Moscow]
nioTimeSystem:   2018-07-02T16:04:03.945726+03:00[Europe/Moscow]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment