OffsetTime minus() method in Java with examples

  1. 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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the minus() method
      
    import java.time.OffsetTime;
    import java.time.temporal.ChronoUnit;
      
    public class GFG {
        public static void main(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));
        }
    }

    chevron_right

    
    

    Output:

    Time after subtraction of hours: 09:35:40+06:03
    

    Program 2 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the minus() method
      
    import java.time.OffsetTime;
    import java.time.temporal.ChronoUnit;
      
    public class GFG {
        public static void main(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));
        }
    }

    chevron_right

    
    

    Output:

    Time after subtraction of minutes: 11:33:40+06:03
    
  2. 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 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the minus() method
      
    import java.time.Duration;
    import java.time.OffsetTime;
      
    public class GFG {
        public static void main(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)));
        }
    }

    chevron_right

    
    

    Output:

    Time after subtraction of hours: 09:35:40+06:03
    

    Program 2 :

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to demonstrate the minus() method
      
    import java.time.Duration;
    import java.time.OffsetTime;
      
    public class GFG {
        public static void main(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)));
        }
    }

    chevron_right

    
    

    Output:

    Time after subtraction of hours: 11:07:40+06:03
    

Reference: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetTime.html#minus-long-java.time.temporal.TemporalUnit-



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

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.