LocalTime ofNanoOfDay() method in Java


A LocalTime object can be obtained using the nanoseconds of the day with the ofNanoOfDay() method in the LocalTime class in Java. This method requires a single parameter i.e. the nanoseconds of the day and it returns the LocalTime object for the nanoseconds of the day

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args){
      long nanoSeconds = 1000000000;
      System.out.println("The nanoseconds of the day: " + nanoSeconds);
      System.out.println("The LocalTime is: " + LocalTime.ofNanoOfDay(nanoSeconds));
   }
}

output

The nanoseconds of the day: 1000000000
The LocalTime is: 00:00:01

Now let us understand the above program.

First the nanoseconds of the day are displayed. Then the LocalTime object is obtained using the nanoseconds of the day with the ofNanoOfDay() method and printed. A code snippet that demonstrates this is as follows:

long nanoSeconds = 1000000000;
System.out.println("The nanoseconds of the day: " + nanoSeconds);
System.out.println("The LocalTime is: " + LocalTime.ofNanoOfDay(nanoSeconds));

Updated on: 30-Jul-2019

60 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements