Java Convert LocalTime to Nanos of Day

Tags: Java LocalTime Java 8

In this Java core tutorial we learn how to convert a java.time.LocalTime object into number of nanos of day in Java programming language.

How to convert a LocalTime to Nanos of Day

With a given LocalTime object in Java we can use the toNanoOfDay() method to extracts the time as nanos of day. In the following Java program we learn how to use the LocalTime.toNanoOfDay() method to convert a LocalTime object into number of nanos of day.

ConvertLocalTimeToNanoOfDayExample1.java

import java.time.LocalTime;

public class ConvertLocalTimeToNanoOfDayExample1 {
    public static void main(String... args) {
        int hour = 1;
        int minute = 10;
        int second = 20;
        int nanoOfSecond = 100;

        LocalTime localTime = LocalTime.of(hour, minute, second, nanoOfSecond);
        long nanoOfDay = localTime.toNanoOfDay();

        System.out.println("LocalTime: " + localTime);
        System.out.println("Nano of Day: " + nanoOfDay);
    }
}
The output as below.
LocalTime: 01:10:20.000000100
Nano of Day: 4220000000100

Happy Coding 😊

Java Convert LocalTime to String

Java Convert LocalTime to Seconds of Day

Java Convert LocalTime to Epoch Seconds

Java Convert LocalTime to LocalDateTime

Java Convert LocalTime to OffsetTime

Java Convert LocalTime to Date

Java Convert LocalTime to Calendar

Java LocalTime by Examples

Java LocalDate by Examples

Java LocalDateTime by Examples

Java Date by Examples

Java Calendar by Examples

Java Calendar using Calendar.Builder by Examples