Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Create, add, and convert Duration values

Create a duration from a minute count or two points in time, add another duration, and read the total in minutes or hours.

Version: Java SE 21

Syntax

import java.time.Duration;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalUnit;

// Static methods declared by Duration
public static Duration between(Temporal startInclusive, Temporal endExclusive)
public static Duration ofMinutes(long minutes)

// Instance methods declared by Duration
public Duration plus(Duration duration)
public Duration plus(long amountToAdd, TemporalUnit unit)
public long toHours()
public long toMinutes()

In Java SE 21, java.time.Duration is an immutable, signed amount of time, not a date or time of day. The static factory Duration.ofMinutes(long minutes) creates a duration from a minute count, which can be negative. Each minute is exactly 60 seconds.

The static method Duration.between(start, end) calculates the difference from the inclusive start to the exclusive end. The result is negative if the end precedes the start. Both inputs must support the SECONDS unit. If their types differ, the calculation uses the first input's type. If the difference in seconds cannot be obtained, the method throws DateTimeException.

For two Duration values, a.plus(b) returns their sum without changing either input; keep the returned value to use the sum. The overload plus(long, TemporalUnit) takes a count and an exact-length unit such as ChronoUnit.MINUTES. The exception is ChronoUnit.DAYS, treated as 24 hours per day. An unsupported unit causes UnsupportedTemporalTypeException. ofMinutes, between, and both plus overloads throw ArithmeticException if the result exceeds the range of Duration.

toMinutes() and toHours() return long totals, not minute or hour components. They divide the stored seconds by 60 or 3600, respectively, using integer division toward zero and ignoring the nanosecond field. For 95 minutes, the totals are 95 and 1, respectively, while toMinutesPart() returns 35.

Examples

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

public class Main {
    public static void main(String[] args) {
        Instant start = Instant.parse("2025-01-01T09:00:00Z");
        Instant end = Instant.parse("2025-01-01T10:15:00Z");
        Duration elapsed = Duration.between(start, end);
        Duration extra = Duration.ofMinutes(20);
        Duration total = elapsed.plus(extra);

        System.out.println("elapsed.toMinutes() = " + elapsed.toMinutes());
        System.out.println("total.toMinutes() = " + total.toMinutes());
        System.out.println("total.toHours() = " + total.toHours());
    }
}

Result

elapsed.toMinutes() = 75
total.toMinutes() = 95
total.toHours() = 1

The fixed instants are 75 minutes apart. Adding 20 minutes gives 95 minutes, or 1 whole hour. `elapsed` still represents 75 minutes.

Related lessons

  • Java · Lesson 68 · Basic · Version: 2

Sources