0Pricing
Java Academy · Lesson

Instant and Duration

Machine time and intervals.

Machine Time

While LocalDateTime is for human calendars, Instant represents a precise point on the machine timeline, measured from the epoch of 1970-01-01 in UTC.

Duration represents a length of time between two instants.

Creating an Instant

Instant.now() captures the current moment. For predictable output we can build one from a fixed epoch second with ofEpochSecond.

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant fixed = Instant.ofEpochSecond(1000000000);
        System.out.println(fixed);
    }
}

All lessons in this course

  1. LocalDate, LocalTime, LocalDateTime
  2. Instant and Duration
  3. Period and Date Arithmetic
  4. Formatting with DateTimeFormatter
← Back to Java Academy