Thursday, June 11, 2026
HomeLanguagesJavaLocalTime ofNanoOfDay() method in Java with Examples

LocalTime ofNanoOfDay() method in Java with Examples

The ofNanoOfDay() method of LocalTime class is used to obtain LocalTime Instance from a nanos-of-day value. This returns a LocalTime with the specified the nano of day, from 0 to 24 * 60 * 60 * 1, 000, 000, 000 – 1 passed as parameter.

Syntax:

public static LocalTime ofNanoOfDay(long nanoOfDay)

Parameters: This method accepts a single parameter nanoOfDay which is the nano of day, from 0 to 24 * 60 * 60 * 1, 000, 000, 000 – 1.

Return value: This method returns LocalTime instance created from the nanoOfDay passed as the parameter.

Exception: This method throws DateTimeException if the nanoofDay value passed as the parameter is invalid.

Below programs illustrate the ofNanoOfDay() method:

Program 1:




// Java program to demonstrate
// LocalTime.ofNanoOfDay() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a nano of day
        long nanoofday = 14245000000000l;
  
        // applying ofNanoOfDay()
        LocalTime value
            = LocalTime.ofNanoOfDay(nanoofday);
  
        // print result
        System.out.println("LocalTime: "
                           + value);
    }
}


Output:

LocalTime: 03:57:25

Program 2:




// Java program to demonstrate
// LocalTime.ofNanoOfDay() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a nano of day
        long nanoofday = 5000000000l;
  
        // applying ofNanoOfDay()
        LocalTime value
            = LocalTime.ofNanoOfDay(nanoofday);
  
        // print result
        System.out.println("LocalTime: "
                           + value);
    }
}


Output:

LocalTime: 00:00:05

References:

RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6896 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6963 POSTS0 COMMENTS