LocalTime toNanoOfDay() method in Java


The time in the form of nanoseconds of the day can be obtained using the toNanoOfDay() method in the LocalTime class in Java. This method requires no parameters and it returns the time in the form of nanoseconds of the day.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.time.*;
public class Main {
   public static void main(String[] args) {
      LocalTime lt = LocalTime.parse("05:15:30");
      System.out.println("The LocalTime is: " + lt);
      System.out.println("The nanoseconds of the day are: " + lt.toNanoOfDay());
   }
}

Output

The LocalTime is: 05:15:30
The nanoseconds of the day are: 18930000000000

Now let us understand the above program.

First the LocalTime is displayed. Then the time in the form of nanoseconds of the day is obtained using the toNanoOfDay() method. A code snippet that demonstrates this is as follows −

LocalTime lt = LocalTime.parse("05:15:30");
System.out.println("The LocalTime is: " + lt);
System.out.println("The nanoseconds of the day are: " + lt.toNanoOfDay());

Updated on: 30-Jul-2019

36 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements