Java Files setLastModifiedTime() Method

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

Definition

In Java, Files.setLastModifiedTime(path, time) sets the last-modified time of a file or folder. Build the value with FileTime.fromMillis(ms), FileTime.from(n, TimeUnit), or (Java 8) FileTime.from(instant).

It is how you "touch" a file, restore a timestamp after editing, or create repeatable test data. The file system may round the value to its precision.

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.setLastModifiedTime(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Path and a FileTime:

Java
Files.setLastModifiedTime(path, FileTime.fromMillis(1_000_000_000_000L));
Files.setLastModifiedTime(path, FileTime.from(1234567890, TimeUnit.SECONDS));
Files.setLastModifiedTime(path, FileTime.from(Instant.parse("2020-01-01T00:00:00Z")));
CallWhat it does
FileTime.fromMillis(ms)From milliseconds since 1970 UTC.
FileTime.from(n, unit)From any TimeUnit.
FileTime.from(instant)From a java.time Instant (Java 8).

Since Java 7. It changes only the modification time; use setAttribute with basic:lastAccessTime or basic:creationTime for the others.

Library declarations
Java
public static Path setLastModifiedTime(Path path, FileTime time) throws IOException

Parameters

ParameterDescription
pathThe file or folder to change.
timeThe new time; must not be null.

Return Value

Return ValueDescription
PathThe path you passed in.

Compare with Similar Methods

Related methods:

MethodHow it differs
getLastModifiedTime()Read the time back.
setAttribute()Also creation and access time.
createFile()Create a file to touch.

Java Examples

Example 1 — Set a Fixed Time and Read It Back

FileTime.fromMillis turns a number into a time the file system can store.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;

public class SetLastModifiedTimeFixed {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("archive.txt"), new byte[0]);
        FileTime when = FileTime.fromMillis(1_000_000_000_000L);
        Files.setLastModifiedTime(file, when);
        FileTime back = Files.getLastModifiedTime(file);
        System.out.println("Set to: " + when);
        System.out.println("Read back: " + back);
        System.out.println("Equal: " + back.equals(when));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Build the value. One trillion milliseconds is a well-known moment in 2001 with no fractional seconds.
  2. Apply it. The file was just created, so its time was "now"; after the call it reports 2001.
  3. Exact round trip. A whole-second value survives on every common file system, so the times are equal.

Example 2 — Seconds with TimeUnit and a java.time Instant

Set the time twice with two different FileTime factories.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class SetLastModifiedTimeUnits {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("log.txt"), new byte[0]);
        Files.setLastModifiedTime(file, FileTime.from(1234567890, TimeUnit.SECONDS));
        System.out.println("From seconds: " + Files.getLastModifiedTime(file));
        Files.setLastModifiedTime(file, FileTime.from(Instant.parse("2020-01-01T00:00:00Z")));
        System.out.println("From Instant: " + Files.getLastModifiedTime(file));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. Unix seconds. FileTime.from(1234567890, SECONDS) is the famous Unix time 1234567890, reached on 13 February 2009.
  2. Readable dates. Instant.parse (Java 8) accepts an ISO-8601 string, which is easier to read than a number.
  3. Last call wins. Each call replaces the previous time, so the second read shows 2020.

Example 3 — Do Milliseconds Survive?

Set a time with 123 milliseconds and check what the file system kept.

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 SetLastModifiedTimePrecision {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path file = Files.write(dir.resolve("precise.txt"), new byte[0]);
        Files.setLastModifiedTime(file, FileTime.fromMillis(1_000_000_000_123L));
        FileTime back = Files.getLastModifiedTime(file);
        System.out.println("Whole seconds kept: " + (back.to(TimeUnit.SECONDS) == 1_000_000_000L));
        System.out.println("Milliseconds kept: " + (back.toMillis() == 1_000_000_000_123L));
        Files.delete(file);
        Files.delete(dir);
    }
}

How It Works

  1. A fractional time. 1,000,000,000,123 ms is 01:46:40.123 on 9 September 2001.
  2. Seconds are safe. Every common file system keeps the whole seconds.
  3. Fractions depend on the disk. NTFS, ext4, and APFS keep milliseconds, so this prints true; FAT, HFS+, and some older JDKs on macOS keep whole seconds only and would print false.

Frequently Asked Questions

Files.setLastModifiedTime(path, FileTime.fromMillis(ms)).
Create it if missing, then call Files.setLastModifiedTime(path, FileTime.from(Instant.now())) to stamp the current time.
FileTime.from(instant) on Java 8, for example FileTime.from(Instant.parse("2020-01-01T00:00:00Z")).
The file system has limited precision; FAT keeps 2-second steps and HFS+ whole seconds.
Use Files.setAttribute(path, "basic:creationTime", time). It works on Windows and macOS; many Linux file systems ignore it.

Next: Files.setOwner()

Change the owner of a file to another UserPrincipal (usually needs admin or root).

setOwner() 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.

13 people found this page helpful