Java Duration minus() Method

The minus() method in Java, part of the java.time.Duration class, is used to subtract a specified amount of time from a Duration instance. This method is useful for calculating durations that are a specified amount of time less than the original duration.

Table of Contents

  1. Introduction
  2. minus() Method Syntax
  3. Understanding minus()
  4. Examples
    • Basic Usage
    • Subtracting Different Temporal Units
  5. Real-World Use Case
  6. Conclusion

Introduction

The minus() method allows you to subtract a specified duration or amount of time from an existing Duration instance. This is particularly useful when you need to adjust a duration by a specific amount of time, such as subtracting minutes, hours, or days.

minus() Method Syntax

The Duration class provides several overloaded minus() methods to subtract various temporal units or another Duration instance. Here are the main variants:

  1. Subtracting a specified duration:
public Duration minus(Duration duration)
  1. Subtracting a specified amount of time:
public Duration minus(long amountToSubtract, TemporalUnit unit)
  1. Subtracting a specified number of days:
public Duration minusDays(long daysToSubtract)
  1. Subtracting a specified number of hours:
public Duration minusHours(long hoursToSubtract)
  1. Subtracting a specified number of minutes:
public Duration minusMinutes(long minutesToSubtract)
  1. Subtracting a specified number of seconds:
public Duration minusSeconds(long secondsToSubtract)
  1. Subtracting a specified number of milliseconds:
public Duration minusMillis(long millisToSubtract)
  1. Subtracting a specified number of nanoseconds:
public Duration minusNanos(long nanosToSubtract)

Understanding minus()

The minus() method creates a new Duration instance by subtracting the specified amount of time from the original duration. The result is a new Duration object representing the adjusted time span.

Examples

Basic Usage

To demonstrate the basic usage of minus(), we will subtract a specified duration from an existing Duration instance.

Example

import java.time.Duration;

public class DurationMinusExample {
    public static void main(String[] args) {
        Duration originalDuration = Duration.ofHours(5);
        Duration subtractedDuration = originalDuration.minus(Duration.ofHours(2));

        System.out.println("Original duration: " + originalDuration);
        System.out.println("Subtracted duration: " + subtractedDuration);
    }
}

Output:

Original duration: PT5H
Subtracted duration: PT3H

Subtracting Different Temporal Units

This example shows how to use different variants of the minus() method to subtract various temporal units from a Duration instance.

Example

import java.time.Duration;

public class DurationSubtractUnitsExample {
    public static void main(String[] args) {
        Duration duration = Duration.ofHours(5);

        // Subtract hours
        Duration result1 = duration.minusHours(2);
        System.out.println("After subtracting 2 hours: " + result1);

        // Subtract minutes
        Duration result2 = duration.minusMinutes(30);
        System.out.println("After subtracting 30 minutes: " + result2);

        // Subtract seconds
        Duration result3 = duration.minusSeconds(120);
        System.out.println("After subtracting 120 seconds: " + result3);

        // Subtract milliseconds
        Duration result4 = duration.minusMillis(60000);
        System.out.println("After subtracting 60000 milliseconds: " + result4);

        // Subtract nanoseconds
        Duration result5 = duration.minusNanos(1000000000);
        System.out.println("After subtracting 1000000000 nanoseconds: " + result5);
    }
}

Output:

After subtracting 2 hours: PT3H
After subtracting 30 minutes: PT4H30M
After subtracting 120 seconds: PT4H58M
After subtracting 60000 milliseconds: PT4H59M
After subtracting 1000000000 nanoseconds: PT4H59M59S

Real-World Use Case

Adjusting Task Durations

In real-world applications, the minus() method can be used to adjust task durations, such as reducing the estimated time for a task by a certain amount when part of the task has already been completed.

Example

import java.time.Duration;

public class TaskDurationAdjustmentExample {
    public static void main(String[] args) {
        Duration estimatedDuration = Duration.ofHours(8);
        Duration timeSpent = Duration.ofHours(3);

        // Adjust the estimated duration by subtracting the time spent
        Duration remainingDuration = estimatedDuration.minus(timeSpent);

        System.out.println("Estimated duration: " + estimatedDuration);
        System.out.println("Time spent: " + timeSpent);
        System.out.println("Remaining duration: " + remainingDuration);
    }
}

Output:

Estimated duration: PT8H
Time spent: PT3H
Remaining duration: PT5H

Conclusion

The Duration.minus() method is used to subtract a specified amount of time from a Duration instance. This method is particularly useful for adjusting durations and calculating time spans. By understanding and using this method, you can effectively manage and manipulate time-based data in your Java applications.

Comments