Home Java Files Methods getLastModifiedTime() Java Files getLastModifiedTime() Method Definition In Java, Files.getLastModifiedTime(path) returns the moment the file's content was last changed, as a java.nio.file.attribute.FileTime.
A FileTime is a point on the timeline, independent of time zones. Convert it with toMillis(), to(TimeUnit), or (Java 8) toInstant(), and compare two values with compareTo.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.getLastModifiedTime(...), after import java.nio.file.Files;. You never create a Files object.
Syntax Path, and optionally NOFOLLOW_LINKS:
FileTime t = Files.getLastModifiedTime(path);
long ms = t.toMillis();
Instant when = t.toInstant(); // Java 8 Call What it does t.toMillis()Milliseconds since 1970-01-01 UTC. t.toInstant()A java.time.Instant (Java 8). a.compareTo(b)Which file changed later.
Since Java 7. FileTime.toString() prints ISO-8601 in UTC, such as 2001-09-09T01:46:40Z.
Library declarations public static FileTime getLastModifiedTime(Path path, LinkOption... options) throws IOException Parameters Parameter Description pathThe file or folder. optionsNOFOLLOW_LINKS for a link's own time.
Return Value Return Value Description FileTimeThe last-modified time.
Important
Throws NoSuchFileException if the file is missing. The precision depends on the file system: NTFS stores 100 ns steps, ext4 and APFS nanoseconds, FAT only 2 seconds. Comparing times from different machines can also be skewed by their clocks.
Compare with Similar Methods Time-related methods:
Java Examples 1. Convert the value 2. Which is newer? 3. Copy and timestamps Example 1 — FileTime as Text, Milliseconds, and a Date Set a known time first so the output is repeatable, then convert it three ways.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.ZoneOffset;
public class GetLastModifiedTimeConvert {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path file = Files.write(dir.resolve("notes.txt"), new byte[0]);
Files.setLastModifiedTime(file, FileTime.fromMillis(1_000_000_000_000L));
FileTime t = Files.getLastModifiedTime(file);
System.out.println("FileTime: " + t);
System.out.println("Millis: " + t.toMillis());
System.out.println("UTC date: " + t.toInstant().atZone(ZoneOffset.UTC).toLocalDate());
Files.delete(file);
Files.delete(dir);
}
} Run with java GetLastModifiedTimeConvert. The time is a fixed value set by the program, so the output is the same on Windows, macOS, and Linux.
FileTime: 2001-09-09T01:46:40Z
Millis: 1000000000000
UTC date: 2001-09-09 How It Works Repeatable input. A real file's time is "whenever it was saved", so the example sets a fixed time first.Raw number. toMillis() returns the value exactly as it was set: one trillion milliseconds.Calendar date. toInstant() bridges to java.time; choosing UTC explicitly keeps the date from depending on the local time zone.Example 2 — Compare Two Files and Measure the Gap Give two files different times, find the newer one, and compute the difference in days.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.util.concurrent.TimeUnit;
public class GetLastModifiedTimeCompare {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path old = Files.write(dir.resolve("old.txt"), new byte[0]);
Path fresh = Files.write(dir.resolve("fresh.txt"), new byte[0]);
Files.setLastModifiedTime(old, FileTime.fromMillis(1_000_000_000_000L));
Files.setLastModifiedTime(fresh, FileTime.fromMillis(1_100_000_000_000L));
FileTime a = Files.getLastModifiedTime(old);
FileTime b = Files.getLastModifiedTime(fresh);
System.out.println("fresh.txt is newer: " + (b.compareTo(a) > 0));
long days = TimeUnit.MILLISECONDS.toDays(b.toMillis() - a.toMillis());
System.out.println("Difference: " + days + " days");
Files.delete(old);
Files.delete(fresh);
Files.delete(dir);
}
} Run with java GetLastModifiedTimeCompare. The time is a fixed value set by the program, so the output is the same on Windows, macOS, and Linux.
fresh.txt is newer: true
Difference: 1157 days How It Works Comparable times. FileTime implements Comparable, so a positive compareTo result means later.The gap. 100,000,000,000 ms is 100,000,000 seconds, about 1157.4 days.Whole days. TimeUnit.toDays drops the fraction, giving 1157. This is how build tools decide whether an output file is out of date.Example 3 — Does a Copy Keep the Original Time? Copy a backdated file once plainly and once with COPY_ATTRIBUTES.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.FileTime;
public class GetLastModifiedTimeCopy {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path src = Files.write(dir.resolve("src.txt"), new byte[4]);
Files.setLastModifiedTime(src, FileTime.fromMillis(1_000_000_000_000L));
Path plain = Files.copy(src, dir.resolve("plain.txt"));
Path kept = Files.copy(src, dir.resolve("kept.txt"), StandardCopyOption.COPY_ATTRIBUTES);
FileTime original = Files.getLastModifiedTime(src);
System.out.println("Plain copy keeps time: " + original.equals(Files.getLastModifiedTime(plain)));
System.out.println("COPY_ATTRIBUTES keeps time: " + original.equals(Files.getLastModifiedTime(kept)));
Files.delete(src);
Files.delete(plain);
Files.delete(kept);
Files.delete(dir);
}
} Run with java GetLastModifiedTimeCopy. First part: typical on Linux and macOS. Last part: Windows.
Plain copy keeps time: false
COPY_ATTRIBUTES keeps time: true
--- On Windows ---
Plain copy keeps time: true
COPY_ATTRIBUTES keeps time: true How It Works A new file gets "now". On Linux and macOS the plain copy is a brand-new file stamped with the moment of copying, not 2001.Windows differs. The Windows copy routine keeps the modification time even without options, so the plain copy matches there.Only one portable choice. COPY_ATTRIBUTES keeps the time everywhere; booleans are printed because the plain copy's real time changes every run.Frequently Asked Questions How do I get the last modified date of a file in Java? FileTime t = Files.getLastModifiedTime(path); then t.toMillis() or t.toInstant().
What time zone does FileTime use? None: it is an instant. toString() prints it in UTC; convert with toInstant().atZone(zone) for local time.
How do I compare the modification times of two files? Files.getLastModifiedTime(a).compareTo(Files.getLastModifiedTime(b)); a positive result means a is newer.
Does Files.copy keep the last modified time? Only StandardCopyOption.COPY_ATTRIBUTES guarantees it. Windows keeps it on a plain copy too; Linux and macOS usually do not. Files.move keeps it.
How is this different from File.lastModified()? java.io.File.lastModified() returns 0 on errors; Files.getLastModifiedTime throws an exception that explains the problem.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
12 people found this page helpful
Helpful Share Copy link Suggestion