Java Files getLastModifiedTime() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

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:

Java
FileTime t = Files.getLastModifiedTime(path);
long ms = t.toMillis();
Instant when = t.toInstant();                                // Java 8
CallWhat 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
Java
public static FileTime getLastModifiedTime(Path path, LinkOption... options) throws IOException

Parameters

ParameterDescription
pathThe file or folder.
optionsNOFOLLOW_LINKS for a link's own time.

Return Value

Return ValueDescription
FileTimeThe last-modified time.

Compare with Similar Methods

Time-related methods:

MethodHow it differs
setLastModifiedTime()Change the time.
readAttributes()Also creation and access time.
copy()Keeps the time reliably only with COPY_ATTRIBUTES.

Java Examples

Example 1 — FileTime as Text, Milliseconds, and a Date

Set a known time first so the output is repeatable, then convert it three ways.

Java
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);
    }
}

How It Works

  1. Repeatable input. A real file's time is "whenever it was saved", so the example sets a fixed time first.
  2. Raw number. toMillis() returns the value exactly as it was set: one trillion milliseconds.
  3. 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.

Java
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);
    }
}

How It Works

  1. Comparable times. FileTime implements Comparable, so a positive compareTo result means later.
  2. The gap. 100,000,000,000 ms is 100,000,000 seconds, about 1157.4 days.
  3. 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.

Java
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);
    }
}

How It Works

  1. 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.
  2. Windows differs. The Windows copy routine keeps the modification time even without options, so the plain copy matches there.
  3. 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

FileTime t = Files.getLastModifiedTime(path); then t.toMillis() or t.toInstant().
None: it is an instant. toString() prints it in UTC; convert with toInstant().atZone(zone) for local time.
Files.getLastModifiedTime(a).compareTo(Files.getLastModifiedTime(b)); a positive result means a is newer.
Only StandardCopyOption.COPY_ATTRIBUTES guarantees it. Windows keeps it on a plain copy too; Linux and macOS usually do not. Files.move keeps it.
java.io.File.lastModified() returns 0 on errors; Files.getLastModifiedTime throws an exception that explains the problem.

Next: Files.getOwner()

Get the user (or group) that owns a file, as a UserPrincipal.

getOwner() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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