In Java, System.currentTimeMillis() returns the current date and time as a long: the number of milliseconds since midnight, January 1, 1970, UTC (the "Unix epoch"). The same number is used by databases, log files, and web APIs, so it is a handy timestamp.
It reads the computer's wall clock, which can jump forward or backward when the clock is synchronised or changed by the user. That is fine for "when did this happen?", but for "how long did this take?" use nanoTime() instead.
A static method of the java.lang.System class, available since the first version of Java (JDK 1.0). Call it on the class name, as in System.currentTimeMillis(...). No import is needed, because classes in java.lang are always visible.
Syntax
No arguments; store the result in a long (an int is far too small):
Java
long now = System.currentTimeMillis(); // e.g. 1795000000000
long seconds = now / 1000; // Unix time in seconds
Instant instant = Instant.ofEpochMilli(now); // java.time view of the same moment
Call
What it does
System.currentTimeMillis()
Milliseconds since 1970-01-01T00:00:00Z.
System.currentTimeMillis() / 1000
Unix time in whole seconds.
Instant.ofEpochMilli(ms)
Turns a millisecond value into a java.time.Instant.
savedAt + 60000 < System.currentTimeMillis()
"Is this more than one minute old?" check.
The value is in UTC and does not depend on the time zone. Its real precision depends on the operating system: some systems only update the clock every few milliseconds.
Library declarations
Java
// java.lang.System
public static native long currentTimeMillis()
Parameters
Parameter
Description
None
currentTimeMillis() takes no parameters.
Return Value
Return Value
Description
long
Milliseconds between the current time and midnight, January 1, 1970 UTC. Currently a 13-digit number.
Arbitrary origin, never adjusted: for measuring elapsed time only.
Instant.now()
Same moment as an Instant object (Java 8+), often with finer precision.
new Date()
Old java.util.Date wrapper around the same milliseconds.
Clock.systemUTC().millis()
Same value, but a Clock can be replaced by a fixed clock in tests.
Java Examples
Example 1 — Check That the Clock Value Makes Sense
The exact value changes all the time, so the program only prints facts that are true for any date after 2024.
Java
public class SystemCurrentTimeMillisEpoch {
public static void main(String[] args) {
long now = System.currentTimeMillis();
long seconds = now / 1000;
long days = now / (24L * 60 * 60 * 1000);
System.out.println("Later than September 2001? " + (now > 1000000000000L));
System.out.println("Seconds value has 10 digits? " + (String.valueOf(seconds).length() == 10));
System.out.println("More than 20000 days since 1970? " + (days > 20000));
}
}
Output
Later than September 2001? true
Seconds value has 10 digits? true
More than 20000 days since 1970? true
How It Works
One trillion milliseconds. 1000000000000 ms after the epoch was September 9, 2001, so any current reading is larger and the first check prints true.
Milliseconds to seconds. Dividing by 1000 gives classic Unix time. It has had 10 digits since 2001 and keeps them until the year 2286.
Milliseconds to days.24L * 60 * 60 * 1000 is 86400000 ms per day (the L keeps the math in long). Day 20000 after the epoch fell in 2024, so the last check is true.
Example 2 — Turn Millisecond Values into Readable Instants
Instant.ofEpochMilli shows what a millisecond value means. Fixed values print the same everywhere; the current time is only round-tripped.
Java
import java.time.Instant;
public class SystemCurrentTimeMillisInstant {
public static void main(String[] args) {
System.out.println("0 -> " + Instant.ofEpochMilli(0));
System.out.println("86400000 -> " + Instant.ofEpochMilli(86400000L));
System.out.println("1000000000000 -> " + Instant.ofEpochMilli(1000000000000L));
long now = System.currentTimeMillis();
Instant moment = Instant.ofEpochMilli(now);
System.out.println("Round trip keeps the value? " + (moment.toEpochMilli() == now));
}
}
The epoch itself. Millisecond 0 is midnight on January 1, 1970; the trailing Z in Instant.toString() means UTC, so no time zone changes the output.
Exactly one day. 86400000 ms is 24 hours, which lands on January 2, 1970 at midnight UTC.
No precision lost. An Instant stores time to the nanosecond, so converting the current millisecond value and back with toEpochMilli() returns the same number.
Example 3 — Decide Whether a Cached Value Is Still Fresh
A typical use: remember when data was saved and compare its age with a limit. All ages are computed from one clock reading, so the result is fixed.
Java
public class SystemCurrentTimeMillisCache {
static final long MAX_AGE_MS = 60000;
static boolean isFresh(long savedAt, long now) {
return now - savedAt < MAX_AGE_MS;
}
public static void main(String[] args) {
long now = System.currentTimeMillis();
long savedRecently = now - 5000;
long savedLongAgo = now - 120000;
System.out.println("Saved 5 s ago, fresh? " + isFresh(savedRecently, now));
System.out.println("Saved 2 min ago, fresh? " + isFresh(savedLongAgo, now));
System.out.println("Age of the old entry in seconds: " + (now - savedLongAgo) / 1000);
}
}
Output
Saved 5 s ago, fresh? true
Saved 2 min ago, fresh? false
Age of the old entry in seconds: 120
How It Works
Store a timestamp. Real code would save System.currentTimeMillis() next to the cached data; here two save times are faked as 5 and 120 seconds before now.
Age = now minus saved. 5000 ms is under the 60000 ms limit, so the first entry is fresh; 120000 ms is over it, so the second is stale.
One reading, fixed results. Passing the same now to every check means the answers cannot change between lines, and dividing 120000 by 1000 prints 120.
Frequently Asked Questions
A long with the number of milliseconds since midnight, January 1, 1970, UTC. It is the same kind of timestamp used by many databases and web APIs.
No. The value is always counted from the epoch in UTC. Time zones only matter when you format it as a date, for example with java.time classes.
Prefer System.nanoTime(). currentTimeMillis follows the wall clock, which can jump when the system time is adjusted, so an elapsed time can even become negative.
Use Instant.ofEpochMilli(millis), then atZone(ZoneId.systemDefault()) for a local date and time, or the older new Date(millis).
Divide by 1000: System.currentTimeMillis() / 1000 gives Unix time in seconds. Instant.now().getEpochSecond() gives the same value.