Skip to content
Related Articles

Related Articles

LocalDateTime minus() method in Java with Examples

Improve Article
Save Article
  • Last Updated : 28 Jan, 2022
Improve Article
Save Article

In LocalDateTime class, there are two types of minus() method depending upon the parameters passed to it.
 

minus(long amountTosubtract, TemporalUnit unit)

minus() method of a LocalDateTime class used to returns a copy of this LocalDateTime with the specified amount of unit subtracted.If it is not possible to subtract the amount, because the unit is not supported or for some other reason, an exception is thrown.
Syntax: 
 

public LocalDateTime minus(long amountToSubtract,
                           TemporalUnit unit)

Parameters: This method accepts two parameters amountToSubtract which is the amount of the unit to subtract to the result, may be negative and unit which is the unit of the amount to subtract, not null.
Return value: This method returns LocalDateTime based on this LocalDateTime with the specified amount subtracted.
Exception: This method throws following Exceptions: 
 

  • DateTimeException – if the subtraction cannot be made
  • UnsupportedTemporalTypeException – if the unit is not supported
  • ArithmeticException – if numeric overflow occurs

Below programs illustrate the minus() method: 
Program 1: 
 

Java




// Java program to demonstrate
// LocalDateTime.minus() method
 
import java.time.*;
import java.time.temporal.ChronoUnit;
 
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the LocalDateTime instance
        LocalDateTime ldt
            = LocalDateTime
                  .parse("2019-12-31T19:15:30");
 
        // Get the String representation of this LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + ldt.toString());
 
        // subtract 200 DAYS to LocalDateTime
        LocalDateTime value
            = ldt.minus(200, ChronoUnit.DAYS);
 
        // print result
        System.out.println("LocalDateTime after subtracting DAYS: "
                           + value);
    }
}

Output: 

Original LocalDateTime: 2019-12-31T19:15:30
LocalDateTime after subtracting DAYS: 2019-06-14T19:15:30

 

minus(TemporalAmount amountTosubtract)

minus() method of a LocalDateTime class used to returns a copy of this LocalDateTime with the specified amount subtracted to date-time.The amount is typically Period or Duration but may be any other type implementing the TemporalAmount interface.
Syntax: 
 

public LocalDateTime minus(TemporalAmount amountTosubtract)

Parameters: This method accepts one single parameter amountTosubtract which is the amount to subtract, It should not be null.
Return value: This method returns LocalDateTime based on this date-time with the subtraction made, not null.
Exception: 
This method throws following Exceptions:

  • DateTimeException – if the subtraction cannot be made
  • ArithmeticException – if numeric overflow occurs

Below programs illustrate the minus() method: 
Program 1: 
 

Java




// Java program to demonstrate
// LocalDateTime.minus() method
 
import java.time.*;
public class GFG {
    public static void main(String[] args)
    {
 
        // Get the LocalDateTime instance
        LocalDateTime ldt
            = LocalDateTime
                  .parse("2019-12-31T19:15:30");
 
        // Get the String representation of this LocalDateTime
        System.out.println("Original LocalDateTime: "
                           + ldt.toString());
 
        // subtract 10 Days to LocalDateTime
        LocalDateTime value
            = ldt.minus(Period.ofDays(10));
 
        // print result
        System.out.println("LocalDateTime after subtracting Days: "
                           + value);
    }
}

Output: 

Original LocalDateTime: 2019-12-31T19:15:30
LocalDateTime after subtracting Days: 2019-12-21T19:15:30

 

References: 
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#minus(java.time.temporal.TemporalAmount) 
https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#minus(long, java.time.temporal.TemporalUnit)
 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!