Determine file age in days

Given a Path, I would like to determine the file's age based on last modified time. I know how to get the last modified time:

FileTime t = Files.getLastModifiedTime(path);

I also know how to get the current date/time with:

LocalDate now = LocalDate.now();

However, I failed to see any connection between the two. I imagine I have to convert from one type to anther, then calculate the diff in days, but I am stuck at reading the documentation at this point. Any help would be greatly appreciated.

Jon Skeet
people
quotationmark

I wouldn't use LocalDate.now() - that depends on your current time zone. A file's age in days of elapsed time can be computed in a time zone neutral manner.

Instead, convert the FileTime to an Instant via toInstant, and then you can find the duration:

Instant fileInstant = t.toInstant();
Instant now = clock.instant(); // Where clock is a java.time.Clock, for testability
Duration difference = Duration.between(fileInstant, now);
long days = difference.toDays();

people

See more on this question at Stackoverflow