Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?

In this article, we will learn how to convert LocalTime to number of Nanoseconds and vice-versa using toNanoOfDay() and ofNanoOfDay() methods of LocalTime respectively provided in Java 1.8 version

Conversion of LocalTime to Nanosecond & vice-versa :

There are 2 methods available in LocalTime for conversion of LocalTime to Nanosecond and vice-versa,

  1. toNanoOfDay() – This method converts LocalTime into Nanoseconds or nano of day ranging from 0 nanoseconds to 86399999999999 nanoseconds
  2. ofNanoOfDay() – This method converts given nanoseconds in long format to LocalTime in the default (HH:mm:ss.nnn) format

1. Convert LocalTime to Nanoseconds :

  • toNanoOfDay() – Extracts the LocalTime as nanos of day, from 0 (zero nanoseconds) to 24 * 60 * 60 * 1,000,000,0001 (8,63,99,99,99,99,999 nanoseconds)

ConvertLocalTimeToNanoseconds.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class ConvertLocalTimeToNanoseconds {

	public static void main(String[] args) {

		// 1. get system time
		LocalTime localTime = LocalTime.now();
		System.out.println("Current System Time is :- \n" + localTime);


		// 2. convert LocalTime to nanoseconds using toNanoOfDay() 
		// between (0 - 8,63,99,99,99,99,999) nanoseconds
		long nanoseconds = localTime.toNanoOfDay();
		System.out.print("\nConversion of LocalTime to Nanoseconds in long format "
				+ "using toNanoOfDay() is :- \n"
				+ nanoseconds);
	}
}

Output:

Current System Time is :- 
08:19:07.381225700

Conversion of LocalTime to Nanoseconds in long format using toNanoOfDay() is :- 
29947381225700

2. Convert Nano of Day to LocalTime :

  • ofNanoOfDay(long) – This method obtains an instance of LocalTime from a nanos-of-day value provided as argument in long format
  • Note:
    1. If the given nanoseconds is greater-than 0 & lesser-than 86399999999999 then this method will return actual time-of-day in default (HH:mm:ss.nnn) format
    2. If the given nanoseconds is exactly 0 then this method will return 00:00
    3. If the given nanoseconds is exactly 86399999999999 then this method will return 23:59:59.999999999
    4. If the given nanoseconds is greater-than 86399999999999 like for example 86499999999999 then this method will throw java.time.DateTimeException stating “Invalid value for NanoOfDay (valid values 0 – 86399999999999): 86499999999999

ConvertNanosecondsToLocalTime.java

package in.bench.resources.java8.localtime.examples;

import java.time.LocalTime;

public class ConvertNanosecondsToLocalTime {

	public static void main(String[] args) {

		// Nanoseconds to LocalTime using LocalTime.ofNanoOfDay()
		System.out.println("Conversion of Nanoseconds to LocalTime using LocalTime.ofNanoOfDay() :-");


		// 1. LocalTime.ofNanoOfDay(76528L) - (greater-than 0 & lesser-than 86399999999999)
		long nanoseconds1 = 75125899139899L;
		LocalTime localTime1 = LocalTime.ofNanoOfDay(nanoseconds1);
		System.out.println("\n1. LocalTime.ofNanoOfDay(75125899139899L) is :- \n" + localTime1);


		// 2. LocalTime.ofNanoOfDay(0L) - exactly 0
		long nanoseconds2 = 0L;
		LocalTime localTime2 = LocalTime.ofNanoOfDay(nanoseconds2);
		System.out.println("\n2. LocalTime.ofNanoOfDay(0L) is :- \n" + localTime2);


		// 3. LocalTime.ofNanoOfDay(86399L) - upper limit 86399999999999
		long nanoseconds3 = 86399999999999L;
		LocalTime localTime3 = LocalTime.ofNanoOfDay(nanoseconds3);
		System.out.println("\n3. LocalTime.ofNanoOfDay(86399999999999L) is :- \n" + localTime3 + "\n\n");


		// 4. LocalTime.ofNanoOfDay(86499999999999L) > 86399999999999
		long nanoseconds4 = 86499999999999L;
		LocalTime localTime4 = LocalTime.ofNanoOfDay(nanoseconds4);
		System.out.print("\n4. LocalTime.ofNanoOfDay(86499999999999L) is :- \n" + localTime4);
	}
}

Output:

Conversion of Nanoseconds to LocalTime using LocalTime.ofNanoOfDay() :-

1. LocalTime.ofNanoOfDay(75125899139899L) is :- 
20:52:05.899139899

2. LocalTime.ofNanoOfDay(0L) is :- 
00:00

3. LocalTime.ofNanoOfDay(86399999999999L) is :- 
23:59:59.999999999


Exception in thread "main" java.time.DateTimeException: 
Invalid value for NanoOfDay (valid values 0 - 86399999999999): 86499999999999
	at java.base/java.time.temporal.ValueRange.checkValidValue(ValueRange.java:319)
	at java.base/java.time.temporal.ChronoField.checkValidValue(ChronoField.java:718)
	at java.base/java.time.LocalTime.ofNanoOfDay(LocalTime.java:400)
	at in.bench.resources.java8.localtime.examples.ConvertNanosecondsToLocalTime
.main(ConvertNanosecondsToLocalTime.java:33)

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
Java 8 – How to convert LocalTime to Seconds and vice-versa ?