- The minus(amountToSubtract, unit) method of OffsetTime class in Java returns a copy of this time with the specified amount subtracted.
Syntax :
public OffsetTime minus(long amountToSubtract, TemporalUnit unit)
Parameter: The method accepts two parameters which are described below:
- amountToSubtract: it is a mandatory parameter which specifies the amount of the unit to subtract from the result, may be negative.
- unit: it is a mandatory parameter which specifies the unit of the amount to subtract, not null.
Return Value: It returns a OffsetTime based on this time with the specified amount subtracted and not null
Below programs illustrate the minus() method:
Program 1 :
// Java program to demonstrate the minus() methodÂÂimportjava.time.OffsetTime;importjava.time.temporal.ChronoUnit;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Parses the time       ÂOffsetTime time = OffsetTime.parse("11:35:40+06:03");       ÂSystem.out.println("Time after subtraction of hours: "                          Â+ time.minus(2, ChronoUnit.HOURS));   Â}}Output:Time after subtraction of hours: 09:35:40+06:03
Program 2 :
// Java program to demonstrate the minus() methodÂÂimportjava.time.OffsetTime;importjava.time.temporal.ChronoUnit;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Parses the time       ÂOffsetTime time = OffsetTime.parse("11:35:40+06:03");       ÂSystem.out.println("Time after subtraction of minutes: "                          Â+ time.minus(2, ChronoUnit.MINUTES));   Â}}Output:Time after subtraction of minutes: 11:33:40+06:03
- The minus(amountToSubtract) method of OffsetTime class in Java returns a copy of this time with the specified amount subtracted.
Syntax :
public OffsetTime minus(TemporalAmount amountToSubtract)
Parameter: This method accepts a single parameter amountToSubtract which specifies the amount to subtract and not null.
Return Value: It returns a OffsetTime based on this time with the subtraction made, not null.
Errors and Exceptions: The program returns two exceptions which are described as below:
- DateTimeException: it is thrown if the subtraction cannot be made.
- ArithmeticException: it is thrown if numeric overflow occurs.
Below programs illustrate the minus() method:
Program 1 :
// Java program to demonstrate the minus() methodÂÂimportjava.time.Duration;importjava.time.OffsetTime;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Parses the time       ÂOffsetTime time = OffsetTime.parse("11:35:40+06:03");       ÂSystem.out.println("Time after subtraction of hours: "                           Â+ time.minus(Duration.ofHours(2)));   Â}}Output:Time after subtraction of hours: 09:35:40+06:03
Program 2 :
// Java program to demonstrate the minus() methodÂÂimportjava.time.Duration;importjava.time.OffsetTime;ÂÂpublicclassGFG {   Âpublicstaticvoidmain(String[] args)   Â{       Â// Parses the time       ÂOffsetTime time = OffsetTime.parse("11:35:40+06:03");       ÂSystem.out.println("Time after subtraction of hours: "                         Â+ time.minus(Duration.ofMinutes(28)));   Â}}Output:Time after subtraction of hours: 11:07:40+06:03
