LocalTime ofNanoOfDay() method in Java

Java 8Object Oriented ProgrammingProgramming

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));
raja
Updated on 30-Jul-2019 22:30:25

Advertisements