LocalDateTime minus() method in Java with Examples - GeeksforGeeks

28 Jan, 2022
In LocalDateTime class, there are two types of minus() method depending upon the parameters passed to it. 
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);    }}
Original LocalDateTime: 2019-12-31T19:15:30
LocalDateTime after subtracting DAYS: 2019-06-14T19:15:30
 
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);    }}
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) 
saurabh1990aror
Java-Functions
Java-LocalDateTime
Java-time package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Interfaces in Java
Stream In Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java