0Pricing
Java Academy · Lesson

Instant and Duration

Machine time and intervals.

Instant and Duration is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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);
    }
}

Epoch Seconds and Millis

You can read the underlying epoch values with getEpochSecond and toEpochMilli. These are useful for timestamps and storage.

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant t = Instant.ofEpochSecond(1500000000);
        System.out.println(t.getEpochSecond());
        System.out.println(t.toEpochMilli());
    }
}

Creating a Duration

Build a Duration with factory methods like ofHours, ofMinutes, and ofSeconds.

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        Duration d = Duration.ofMinutes(90);
        System.out.println(d);
        System.out.println(d.toHours());
    }
}

Duration Between Instants

Duration.between measures the gap between two instants. This is the standard way to time how long something took.

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant start = Instant.ofEpochSecond(1000);
        Instant end = Instant.ofEpochSecond(1075);
        Duration elapsed = Duration.between(start, end);
        System.out.println(elapsed.getSeconds());
    }
}

Converting Durations

A Duration can report itself in different units with toMinutes, toHours, toDays, and toMillis.

import java.time.Duration;

public class Main {
    public static void main(String[] args) {
        Duration d = Duration.ofSeconds(7320);
        System.out.println(d.toMinutes());
        System.out.println(d.toHours());
    }
}

Adding to an Instant

Shift an instant forward or backward with plus and minus, passing a Duration.

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant now = Instant.ofEpochSecond(1000);
        Instant later = now.plus(Duration.ofHours(2));
        System.out.println(later.getEpochSecond());
    }
}

Plus Helper Methods

Convenience methods such as plusSeconds and plusMillis let you skip building a Duration for simple shifts.

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant t = Instant.ofEpochSecond(0);
        System.out.println(t.plusSeconds(3600).getEpochSecond());
    }
}

Comparing Instants

Use isBefore and isAfter to order two instants on the timeline.

import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Instant a = Instant.ofEpochSecond(100);
        Instant b = Instant.ofEpochSecond(200);
        System.out.println(a.isBefore(b));
    }
}

Negative Durations

If the end is earlier than the start, the duration is negative. isNegative and abs help you handle this.

import java.time.Duration;
import java.time.Instant;

public class Main {
    public static void main(String[] args) {
        Duration d = Duration.between(Instant.ofEpochSecond(200), Instant.ofEpochSecond(100));
        System.out.println(d.isNegative());
        System.out.println(d.abs().getSeconds());
    }
}

Instant Versus LocalDateTime

Use Instant for timestamps, logging, and measuring elapsed time, since it is unambiguous UTC. Use LocalDateTime for human calendar values where time zone is handled elsewhere.

Quick Check

Test your understanding of Instant and Duration.

Recap

You learned about machine time:

  • Instant is a precise point on the UTC timeline from the 1970 epoch
  • Duration is a time-based amount built with ofHours and friends
  • Duration.between measures elapsed time
  • Shift instants with plus and minus
  • Use Instant for timestamps, LocalDateTime for human dates

Frequently asked questions

Is the “Instant and Duration” lesson free?

Yes — the full text of “Instant and Duration” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Instant and Duration”?

Machine time and intervals. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Instant and Duration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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