Saturday, October 25, 2025
HomeLanguagesJavaOffsetTime minus() method in Java with examples

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 :




    // 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-

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS