OffsetTime minus() method in Java with examples
- 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() methodimportjava.time.OffsetTime;importjava.time.temporal.ChronoUnit;publicclassGFG {publicstaticvoidmain(String[] args){// Parses the timeOffsetTime time = OffsetTime.parse("11:35:40+06:03");System.out.println("Time after subtraction of hours: "+ time.minus(2, ChronoUnit.HOURS));}}chevron_rightfilter_noneOutput:Time after subtraction of hours: 09:35:40+06:03
Program 2 :
// Java program to demonstrate the minus() methodimportjava.time.OffsetTime;importjava.time.temporal.ChronoUnit;publicclassGFG {publicstaticvoidmain(String[] args){// Parses the timeOffsetTime time = OffsetTime.parse("11:35:40+06:03");System.out.println("Time after subtraction of minutes: "+ time.minus(2, ChronoUnit.MINUTES));}}chevron_rightfilter_noneOutput: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() methodimportjava.time.Duration;importjava.time.OffsetTime;publicclassGFG {publicstaticvoidmain(String[] args){// Parses the timeOffsetTime time = OffsetTime.parse("11:35:40+06:03");System.out.println("Time after subtraction of hours: "+ time.minus(Duration.ofHours(2)));}}chevron_rightfilter_noneOutput:Time after subtraction of hours: 09:35:40+06:03
Program 2 :
// Java program to demonstrate the minus() methodimportjava.time.Duration;importjava.time.OffsetTime;publicclassGFG {publicstaticvoidmain(String[] args){// Parses the timeOffsetTime time = OffsetTime.parse("11:35:40+06:03");System.out.println("Time after subtraction of hours: "+ time.minus(Duration.ofMinutes(28)));}}chevron_rightfilter_noneOutput:Time after subtraction of hours: 11:07:40+06:03
Recommended Posts:
- OffsetTime from() method in Java with examples
- OffsetTime now() method in Java with examples
- OffsetTime get() method in Java with examples
- OffsetTime until() Method in Java with Examples
- OffsetTime plus() method in Java with examples
- OffsetTime with() Method in Java with Examples
- LocalDateTime minus() method in Java with Examples
- ChronoPeriod minus() method in Java with Examples
- Instant minus() method in Java with Examples
- LocalDate minus() method in Java with Examples
- ZonedDateTime minus() method in Java with Examples
- Period minus() method in Java with Examples
- DayOfWeek minus() method in Java with Examples
- LocalTime minus() method in Java with Examples
- OffsetTime toLocalTime() method in Java with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

