Java Duration API Guide

Introduction

The Duration class in Java 8, part of the java.time package, represents a time-based amount of time, such as '34.5 seconds'. It can be used to measure time intervals in nanoseconds and seconds. The Duration class is immutable and thread-safe, making it a robust choice for handling time intervals in applications.

Key Points:

  • Immutability: Immutable and thread-safe.
  • Precision: Can represent durations down to nanoseconds.
  • Arithmetic Operations: Supports addition, subtraction, and other arithmetic operations.
  • Compatibility: Can be used with other Java 8 date and time classes.

Table of Contents

  1. Creating a Duration
  2. Methods of Duration Class
  3. Comparing Durations
  4. Manipulating Durations
  5. Converting Durations
  6. Using Duration with Date-Time APIs
  7. Examples
  8. Conclusion

1. Creating a Duration

Using of Methods

The Duration class provides various of methods to create durations of different time units.

Duration duration1 = Duration.ofDays(1); // 1 day
Duration duration2 = Duration.ofHours(2); // 2 hours
Duration duration3 = Duration.ofMinutes(30); // 30 minutes
Duration duration4 = Duration.ofSeconds(45); // 45 seconds
Duration duration5 = Duration.ofMillis(500); // 500 milliseconds
Duration duration6 = Duration.ofNanos(1000000); // 1 millisecond in nanoseconds

Using between

Creates a duration between two Temporal objects (e.g., two Instant objects).

Instant start = Instant.now();
Instant end = start.plus(Duration.ofHours(1));
Duration duration = Duration.between(start, end);
System.out.println("Duration between start and end: " + duration);

2.() Methods of Duration Class

getSeconds()

Returns the number of seconds in the duration.

long seconds = duration.getSeconds();
System.out.println("Seconds: " + seconds);

getNano()

Returns the nanoseconds part of the duration.

int nanos = duration.getNano();
System.out.println("Nanoseconds: " + nanos);

toDays()

Converts the duration to days.

long days = duration.toDays();
System.out.println("Days: " + days);

toHours()

Converts the duration to hours.

long hours = duration.toHours();
System.out.println("Hours: " + hours);

toMinutes()

Converts the duration to minutes.

long minutes = duration.toMinutes();
System.out.println("Minutes: " + minutes);

toMillis()

Converts the duration to milliseconds.

long millis = duration.toMillis();
System.out.println("Milliseconds: " + millis);

toNanos()

Converts the duration to nanoseconds.

long nanos = duration.toNanos();
System.out.println("Nanoseconds: " + nanos);

3. Comparing Durations

isZero()

Checks if the duration is zero.

boolean isZero = duration.isZero();
System.out.println("Is duration zero? " + isZero);

isNegative()

Checks if the duration is negative.

boolean isNegative = duration.isNegative();
System.out.println("Is duration negative? " + isNegative);

compareTo()

Compares this duration to another duration.

Duration duration1 = Duration.ofMinutes(5);
Duration duration2 = Duration.ofMinutes(10);
int comparison = duration1.compareTo(duration2);
System.out.println("Comparison: " + comparison); // -1, 0, or 1

4. Manipulating Durations

plus()

Adds the specified duration to this duration.

Duration longerDuration = duration.plus(Duration.ofMinutes(10));
System.out.println("Longer Duration: " + longerDuration);

minus()

Subtracts the specified duration from this duration.

Duration shorterDuration = duration.minus(Duration.ofMinutes(10));
System.out.println("Shorter Duration: " + shorterDuration);

multipliedBy()

Multiplies this duration by the specified value.

Duration multipliedDuration = duration.multipliedBy(2);
System.out.println("Multiplied Duration: " + multipliedDuration);

dividedBy()

Divides this duration by the specified value.

Duration dividedDuration = duration.dividedBy(2);
System.out.println("Divided Duration: " + dividedDuration);

5. Converting Durations

toString()

Converts the duration to an ISO-8601 string representation.

String durationString = duration.toString();
System.out.println("Duration as String: " + durationString);

parse()

Parses an ISO-8601 string representation to create a duration.

Duration parsedDuration = Duration.parse("PT1H30M"); // 1 hour and 30 minutes
System.out.println("Parsed Duration: " + parsedDuration);

6. Using Duration with Date-Time APIs

Adding and Subtracting Duration to Instant

Instant now = Instant.now();
Instant later = now.plus(Duration.ofHours(1));
Instant earlier = now.minus(Duration.ofHours(1));
System.out.println("Now: " + now);
System.out.println("One hour later: " + later);
System.out.println("One hour earlier: " + earlier);

Using Duration with ChronoUnit

Duration duration = Duration.of(1, ChronoUnit.HOURS);
System.out.println("Duration in Hours: " + duration);

7. Examples

Example 1: Creating a Duration

Duration duration = Duration.ofMinutes(90);
System.out.println("Duration: " + duration);

Example 2: Comparing Durations

Duration duration1 = Duration.ofMinutes(30);
Duration duration2 = Duration.ofMinutes(60);
System.out.println("Is duration1 zero? " + duration1.isZero());
System.out.println("Is duration1 negative? " + duration1.isNegative());
System.out.println("Comparison: " + duration1.compareTo(duration2));

Example 3: Manipulating Durations

Duration duration = Duration.ofMinutes(30);
Duration longerDuration = duration.plus(Duration.ofMinutes(10));
Duration shorterDuration = duration.minus(Duration.ofMinutes(10));
System.out.println("Longer Duration: " + longerDuration);
System.out.println("Shorter Duration: " + shorterDuration);

Example 4: Converting Duration to Other Units

Duration duration = Duration.ofMinutes(90);
long hours = duration.toHours();
long minutes = duration.toMinutes();
long millis = duration.toMillis();
System.out.println("Hours: " + hours);
System.out.println("Minutes: " + minutes);
System.out.println("Milliseconds: " + millis);

Example 5: Using Duration with Instant

Instant now = Instant.now();
Duration duration = Duration.ofHours(1);
Instant later = now.plus(duration);
Instant earlier = now.minus(duration);
System.out.println("Now: " + now);
System.out.println("One hour later: " + later);
System.out.println("One hour earlier: " + earlier);

8. Conclusion

The Duration class in Java 8 provides a powerful and flexible way to work with time-based amounts of time. Its immutability, precision, and easy interoperability with other date-time classes make it a valuable tool for handling time intervals in Java applications.

Summary of Key Points:

  • Immutability: Immutable and thread-safe.
  • Precision: Can represent durations down to nanoseconds.
  • Arithmetic Operations: Supports addition, subtraction, multiplication, and division.
  • Interoperability: Easily converts to and from other date-time types.

By understanding and utilizing the Duration class effectively, you can manage time intervals in your Java applications with precision and reliability.

Comments