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,
- toNanoOfDay() – This method converts LocalTime into Nanoseconds or nano of day ranging from 0 nanoseconds to 86399999999999 nanoseconds
- 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,000 – 1 (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:
- 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
- If the given nanoseconds is exactly 0 then this method will return 00:00
- If the given nanoseconds is exactly 86399999999999 then this method will return 23:59:59.999999999
- 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:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!