Duration class

The java.time.Duration class models a time-based amount of time measured in seconds and nanoseconds. It is ideal for measuring time between two Instant values or defining fixed time periods (e.g., wait 5 seconds, time out after 2 minutes).

Commonly Used Methods 

Simple Program

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

public class SimpleDurationExample {
    public static void main(String[] args) {
        Instant start = Instant.now();

        // Simulate some delay
        try {
            Thread.sleep(1000); // 1 second delay
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Instant end = Instant.now();
        Duration duration = Duration.between(start, end);

        System.out.println("Time elapsed in seconds: " + duration.getSeconds());
        System.out.println("Time elapsed in milliseconds: " + duration.toMillis());
    }
}

Problem Statement

LotusJavaPrince and LotusPythonPrince are working on a performance monitoring system for their online banking app at BankSecure Ltd. They need to:

  • Monitor the processing duration of each API call.
  • Trigger an alert if the call takes longer than the allowed SLA of 2 seconds.
  • Use Duration to compute, compare, and report the processing time of each API call.
import java.time.Duration;
import java.time.Instant;

public class ApiPerformanceMonitor {

    public static void main(String[] args) {
        // Define SLA limit duration
        Duration slaLimit = Duration.ofSeconds(2);

        // Simulate API processing
        Instant startTime = Instant.now();
        simulateApiProcessing(); // API work
        Instant endTime = Instant.now();

        // Calculate the processing duration
        Duration processingTime = Duration.between(startTime, endTime);

        System.out.println("API started at: " + startTime);
        System.out.println("API ended at: " + endTime);
        System.out.println("API processing time (ms): " + processingTime.toMillis());

        // Compare against SLA
        if (processingTime.compareTo(slaLimit) > 0) {
            System.out.println("SLA breached! Processing took longer than 2 seconds.");
        } else {
            System.out.println("SLA met. Processing time is within acceptable limits.");
        }
    }

    private static void simulateApiProcessing() {
        try {
            Thread.sleep(2500); // Simulate slow API
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
/*
API started at: 2025-05-22T12:00:00.100Z
API ended at: 2025-05-22T12:00:02.620Z
API processing time (ms): 2520
SLA breached! Processing took longer than 2 seconds.
*/

The Duration class in Java offers a precise way to measure or represent time intervals in terms of seconds and nanoseconds. It is:

  • Ideal for comparing execution times.
  • Useful in setting timeouts, delays, retries, and performance monitoring.
  • Works seamlessly with classes like Instant and LocalTime.

In systems where performance, response time, or scheduling is crucial (like real-time apps, banking APIs, or event handling), Duration provides a powerful and clean solution.

Scroll to Top