Saturday, December 6, 2025
HomeLanguagesJavaOffsetTime minusHours() method in Java with examples

OffsetTime minusHours() method in Java with examples

The minusHours() method of OffsetTime class in Java returns a copy of this OffsetTime with the specified hours in the parameter getting subtracted.

Syntax :

public OffsetTime minusHours(long hours)

Parameter: This method accepts a single parameter hours which the hours to be subtracted. It may be negative.

Return Value: It returns a OffsetTime based on this time with the hours subtracted and it is not null.

Below programs illustrate the minusHours() method:

Program 1 :




// Java program to demonstrate the minusHours() method
  
import java.time.OffsetTime;
  
public class GFG {
    public static void main(String[] args)
    {
        // Parses the time
        OffsetTime time = OffsetTime.parse("11:10:10+05:05");
  
        // Specified hours subtracted
        System.out.println("After subtraction, time is: " 
                                      + time.minusHours(5));
    }
}


Output:

After subtraction, time is: 06:10:10+05:05

Program 2 :




// Java program to demonstrate the minusHours() method
  
import java.time.OffsetTime;
  
public class GFG {
    public static void main(String[] args)
    {
        // Parses the time
        OffsetTime time = OffsetTime.parse("11:10:10+05:05");
  
        // Specified hours subtracted
        System.out.println("After subtraction, time is: " 
                                    + time.minusHours(-5));
    }
}


Output:

After subtraction, time is: 16:10:10+05:05

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

RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32427 POSTS0 COMMENTS
Milvus
103 POSTS0 COMMENTS
Nango Kala
6803 POSTS0 COMMENTS
Nicole Veronica
11944 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12013 POSTS0 COMMENTS
Shaida Kate Naidoo
6932 POSTS0 COMMENTS
Ted Musemwa
7185 POSTS0 COMMENTS
Thapelo Manthata
6881 POSTS0 COMMENTS
Umr Jansen
6867 POSTS0 COMMENTS