Sub topic 7
Instant: A Point on the Timeline
The earlier chapters covered LocalDate, LocalTime, and LocalDateTime. Those types describe a date or time the way a wall calendar or a clock face does. They don't say where in the world that time happens. Sometimes you need to name one exact moment that is the same for everyone on Earth. For that, Java gives you Instant.
What an Instant Is
An Instant is a single point on the timeline. It is stored as a count of seconds (plus nanoseconds) from a fixed starting point.
That starting point is called the epoch. The epoch is midnight at the very start of January 1, 1970, in UTC. UTC (Coordinated Universal Time) is the world's reference time. It has no time zone shifts and no daylight saving time.
An Instant is the same moment no matter where you are. When it is 13:00Z in UTC, it is 9:00 in New York and 22:00 in Tokyo, and all three are the same Instant. The Z at the end means "UTC".
When to Use Instant
Use Instant when the computer cares about when something happened and people don't need to read it as a local time. For example:
- Timestamps in logs
- The time a record was saved to a database
- Measuring how long a task took
Use LocalDate or LocalDateTime when people care about the calendar, such as a birthday or an alarm at 7:00.
A Worked Example
import java.time.Instant;
public class Instants {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now); // the current moment in UTC; changes every run
Instant epoch = Instant.ofEpochSecond(0);
System.out.println(epoch); // 1970-01-01T00:00:00Z
Instant meeting = Instant.parse("2024-07-01T13:00:00Z");
System.out.println(meeting.getEpochSecond()); // 1719838800
System.out.println(meeting.toEpochMilli()); // 1719838800000
Instant oneHourLater = meeting.plusSeconds(3600);
System.out.println(oneHourLater); // 2024-07-01T14:00:00Z
System.out.println(meeting.isBefore(oneHourLater)); // true
}
}Here is what each part does:
Instant.now()reads the current moment from the system clock.Instant.ofEpochSecond(0)builds the instant zero seconds after the epoch, which is the epoch itself.Instant.parse(...)reads text in the standard ISO-8601 format. ISO-8601 is the international standard way to write dates and times. The text must end inZor an offset like+02:00.getEpochSecond()andtoEpochMilli()return the raw count in seconds or milliseconds.plusSeconds(3600)returns a newInstant.meetingitself does not change, just like the other java.time types.
What Instant Does Not Have
An Instant has no year, month, or day-of-week fields you can ask for directly. It is just a position on the timeline. To show it as "Monday, 9:00 in New York", you must add a time zone. The next chapter explains how.
Exercise
What does System.out.println(Instant.ofEpochSecond(86400)); print, and why?
Show answer
It prints 1970-01-02T00:00:00Z. A day has 86,400 seconds (24 × 60 × 60), so the result is exactly one day after the epoch, in UTC.
Exercise
An app records the moment each user clicks "Buy". Should it store a LocalDateTime or an Instant? Explain.
Show answer
It should store an Instant. A click happens at one exact moment worldwide. A LocalDateTime has no time zone, so clicks from different countries could not be compared or ordered correctly.