Thursday, January 22, 2026
HomeLanguagesJavaOffsetTime minusNanos() method in Java with examples

OffsetTime minusNanos() method in Java with examples

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

Syntax :

public OffsetTime minusNanos(long Nanos)

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

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

Below programs illustrate the minusNanos() method:

Program 1 :




// Java program to demonstrate the minusNanos() 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 Nanos subtracted
        System.out.println("After subtraction, time is: " + time.minusNanos(5));
    }
}


Output:

After subtraction, time is: 11:10:09.999999995+05:05

Program 2 :




// Java program to demonstrate the minusNanos() 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 Nanos subtracted
        System.out.println("After subtraction, time is: " + time.minusNanos(-5));
    }
}


Output:

After subtraction, time is: 11:10:10.000000005+05:05

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

RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS