Skip to content
Related Articles

Related Articles

Improve Article

OffsetTime minus() method in Java with examples

  • Last Updated : 31 Dec, 2018
  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 :






    // 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));
        }
    }
    Output:
    Time after subtraction of hours: 09:35:40+06:03
    

    Program 2 :




    // 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));
        }
    }
    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 :




    // 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)));
        }
    }
    Output:
    Time after subtraction of hours: 09:35:40+06:03
    

    Program 2 :




    // 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)));
        }
    }
    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-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :