Java Duration

Introduction

Duration in Java, part of the java.time package, represents a time-based amount of time, such as '34.5 seconds'. It is used to measure time spans in terms of seconds and nanoseconds.

Table of Contents

  1. What is Duration?
  2. Creating Duration Instances
  3. Common Methods
  4. Examples of Duration
  5. Conclusion

1. What is Duration?

Duration is used to represent an amount of time in seconds and nanoseconds. It can be used to measure the difference between two instants or to manipulate time.

2. Creating Duration Instances

You can create Duration instances in several ways:

  • Duration.ofSeconds(long seconds)
  • Duration.ofMinutes(long minutes)
  • Duration.ofHours(long hours)
  • Duration.ofDays(long days)
  • Duration.between(Temporal startInclusive, Temporal endExclusive)

3. Common Methods

  • getSeconds(): Returns the whole seconds part of the duration.
  • toMinutes(): Converts the duration to minutes.
  • toHours(): Converts the duration to hours.
  • toDays(): Converts the duration to days.
  • plus(Duration duration): Adds the specified duration.
  • minus(Duration duration): Subtracts the specified duration.
  • multipliedBy(long multiplicand): Multiplies the duration by a scalar.
  • dividedBy(long divisor): Divides the duration by a scalar.
  • isZero(): Checks if the duration is zero.
  • isNegative(): Checks if the duration is negative.
  • abs(): Returns the absolute value of the duration.
  • negated(): Returns the negated duration.

4. Examples of Duration

Example 1: Creating a Duration of Seconds

import java.time.Duration;

public class DurationOfSecondsExample {
    public static void main(String[] args) {
        Duration duration = Duration.ofSeconds(60);
        System.out.println("Duration in Seconds: " + duration.getSeconds());
    }
}

Output:

Duration in Seconds: 60

Example 2: Calculating Duration Between Two Instants

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

public class DurationBetweenExample {
    public static void main(String[] args) {
        Instant start = Instant.now();
        Instant end = start.plusSeconds(120);
        Duration duration = Duration.between(start, end);
        System.out.println("Duration in Seconds: " + duration.getSeconds());
    }
}

Output:

Duration in Seconds: 120

Example 3: Adding and Subtracting Durations

import java.time.Duration;

public class AddSubtractDurationExample {
    public static void main(String[] args) {
        Duration duration1 = Duration.ofMinutes(30);
        Duration duration2 = Duration.ofMinutes(15);
        Duration result = duration1.plus(duration2);
        System.out.println("Duration after Addition: " + result.toMinutes() + " minutes");

        result = result.minus(duration2);
        System.out.println("Duration after Subtraction: " + result.toMinutes() + " minutes");
    }
}

Output:

Duration after Addition: 45 minutes
Duration after Subtraction: 30 minutes

Example 4: Multiplying and Dividing Durations

import java.time.Duration;

public class MultiplyDivideDurationExample {
    public static void main(String[] args) {
        Duration duration = Duration.ofMinutes(10);
        Duration multiplied = duration.multipliedBy(2);
        System.out.println("Duration after Multiplication: " + multiplied.toMinutes() + " minutes");

        Duration divided = duration.dividedBy(2);
        System.out.println("Duration after Division: " + divided.toMinutes() + " minutes");
    }
}

Output:

Duration after Multiplication: 20 minutes
Duration after Division: 5 minutes

Example 5: Checking if a Duration is Zero or Negative

import java.time.Duration;

public class DurationCheckExample {
    public static void main(String[] args) {
        Duration duration = Duration.ofSeconds(-10);
        System.out.println("Is Duration Zero? " + duration.isZero());
        System.out.println("Is Duration Negative? " + duration.isNegative());
    }
}

Output:

Is Duration Zero? false
Is Duration Negative? true

Example 6: Absolute and Negated Duration

import java.time.Duration;

public class AbsNegatedDurationExample {
    public static void main(String[] args) {
        Duration duration = Duration.ofSeconds(-10);
        Duration absDuration = duration.abs();
        System.out.println("Absolute Duration: " + absDuration.getSeconds() + " seconds");

        Duration negatedDuration = duration.negated();
        System.out.println("Negated Duration: " + negatedDuration.getSeconds() + " seconds");
    }
}

Output:

Absolute Duration: 10 seconds
Negated Duration: 10 seconds

Conclusion

The Duration class in Java is used to represent and manipulate time-based amounts. It is particularly useful for measuring time intervals and performing arithmetic on durations. Using Duration can lead to more precise and readable code when working with time spans.

Comments