Most time-related bugs come from mixing three concepts: an absolute instant, human-facing wall time, and a monotonic clock used to measure elapsed duration. They may all be represented by numbers, but they are not interchangeable.
1. What is UTC?
UTC is the global time standard. It does not belong to a particular region and does not observe daylight-saving time. The Z in 2026-10-08T08:00:00Z means UTC.
A Unix timestamp counts seconds or milliseconds from 1970-01-01T00:00:00Z. One timestamp identifies the same instant everywhere and contains no timezone:
- Server protocols often use epoch seconds, such as
1791446400. - Java, JavaScript, and Android often use epoch milliseconds, such as
1791446400000. - A timezone is applied only when that instant is rendered as calendar fields.
Current values often have 10 digits in seconds and 13 in milliseconds, but that heuristic is unsafe for historical and far-future values. Name fields explicitly, such as epochSeconds or epochMillis.
2. What is wall time?
Wall time is the year, month, day, and clock value shown to a person, such as “October 8, 2026 at 16:00.” It depends on timezone rules, daylight-saving time, and the device clock.
Wall time can jump forward or backward after a manual clock change, network synchronization, or a daylight-saving transition. Therefore:
- Use wall time or Unix timestamps for logs, server fields, and calendar display.
- Use a monotonic clock for request duration, animation intervals, and timeouts.
- Do not measure short operations by subtracting wall-clock readings when clock adjustments matter.
3. Getting timestamps in Java
Java can read Unix time through System.currentTimeMillis() or java.time.Instant:
long epochMillis = System.currentTimeMillis();
Instant now = Instant.now();
long epochSeconds = now.getEpochSecond();
long epochMillisFromInstant = now.toEpochMilli();
Both System.currentTimeMillis() and Instant.now() are wall-clock readings and can be affected by system clock corrections. Inject a Clock when business logic needs deterministic tests:
Instant now = Instant.now(clock);
Use the monotonic System.nanoTime() to measure code execution:
long started = System.nanoTime();
runTask();
long elapsedNanos = System.nanoTime() - started;
The nanoTime() value has no Unix epoch meaning. Do not format it as a date or compare it across devices.
4. Common Android timestamps and clocks
Android exposes both wall-clock timestamps and monotonic time since boot:
| API | Clock type | Typical use |
|---|---|---|
System.currentTimeMillis() |
Unix milliseconds, wall time | Logs, API fields, persisted dates |
Instant.now().toEpochMilli() |
Unix milliseconds, wall time | Absolute instants with java.time |
SystemClock.elapsedRealtime() |
Milliseconds since boot, includes deep sleep | Timeouts, countdowns, duration across sleep |
SystemClock.elapsedRealtimeNanos() |
Nanoseconds since boot, includes deep sleep | Precise duration and sensor alignment |
SystemClock.uptimeMillis() |
Milliseconds since boot, excludes deep sleep | Handler, UI scheduling, active-runtime timing |
Native Instant support starts at Android API 26. Lower versions can use it when core library desugaring is enabled.
AlarmManager RTC types follow wall time, while ELAPSED_REALTIME types follow the monotonic boot clock. Android locations also expose both domains: Location.getTime() is Unix milliseconds, while Location.getElapsedRealtimeNanos() is useful for measuring the age of a fix.
The practical rule is simple: use Unix time when a value must correspond to a server or calendar; use elapsedRealtime or nanoTime when the question is only “how much time has passed?”