In this article, we will learn how to convert LocalTime to number of Seconds and vice-versa using toSecondOfDay() and ofSecondOfDay() methods of LocalTime respectively provided in Java 1.8 version
Conversion of LocalTime to Second & vice-versa :
There are 2 methods available in LocalTime for conversion of LocalTime to Second and vice-versa,
- toSecondOfDay() – This method converts LocalTime into Seconds or seconds of day ranging from 0 seconds to 86399 seconds
- ofSecondOfDay(long) – This method converts given seconds in long format to LocalTime in the default (HH:mm:ss) format
1. Convert LocalTime to Seconds :
- toSecondOfDay() – Extracts the LocalTime as seconds of day, from
0
(zero second) to 24 * 60 * 60 – 1 (86399 seconds)
ConvertLocalTimeToSeconds.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
public class ConvertLocalTimeToSeconds {
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 Seconds using toSecondOfDay() between (0 - 86399) seconds
int seconds = localTime.toSecondOfDay();
System.out.print("\nConversion of LocalTime to Seconds in long format "
+ "using toSecondOfDay() is :- \n"
+ seconds);
}
}
Output:
Current System Time is :-
20:39:51.150965300
Conversion of LocalTime to Seconds in long format using toSecondOfDay() is :-
74391
2. Convert Second of Day to LocalTime :
- ofSecondOfDay(long) – This method obtains an instance of
LocalTime
from a second-of-day value provided as argument in long format - Note:
- If the given seconds is greater-than 0 & lesser-than 86399 then this method will return actual time-of-day in default (HH:mm:ss) format
- If the given seconds is exactly 0 then this method will return 00:00
- If the given seconds is exactly 86399 then this method will return 23:59:59
- If the given seconds is greater-than 86399 like for example 86499 then this method will throw java.time.DateTimeException stating “Invalid value for SecondOfDay (valid values 0 – 86399): 86499“
ConvertSecondsToLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.time.LocalTime;
public class ConvertSecondsToLocalTime {
public static void main(String[] args) {
// Seconds to LocalTime using LocalTime.ofSecondOfDay()
System.out.println("Conversion of Seconds to LocalTime using LocalTime.ofSecondOfDay() :-");
// 1. LocalTime.ofSecondOfDay(76528L) > 0
long seconds1 = 76528L;
LocalTime localTime1 = LocalTime.ofSecondOfDay(seconds1);
System.out.println("\n1. LocalTime.ofSecondOfDay(76528L) is :- \n" + localTime1);
// 2. LocalTime.ofSecondOfDay(0L) - exactly 0
long seconds2 = 0L;
LocalTime localTime2 = LocalTime.ofSecondOfDay(seconds2);
System.out.println("\n2. LocalTime.ofSecondOfDay(0L) is :- \n" + localTime2);
// 3. LocalTime.ofSecondOfDay(86399L) - upper limit 86399
long seconds3 = 86399L;
LocalTime localTime3 = LocalTime.ofSecondOfDay(seconds3);
System.out.println("\n3. LocalTime.ofSecondOfDay(86399L) is :- \n" + localTime3 + "\n\n");
// 4. LocalTime.ofSecondOfDay(86499L) > 86399
long seconds4 = 86499L;
LocalTime localTime4 = LocalTime.ofSecondOfDay(seconds4);
System.out.print("\n4. LocalTime.ofSecondOfDay(86499L) is :- \n" + localTime4);
}
}
Output:
Conversion of Seconds to LocalTime using LocalTime.ofSecondOfDay() :-
1. LocalTime.ofSecondOfDay(76528L) is :-
21:15:28
2. LocalTime.ofSecondOfDay(0L) is :-
00:00
3. LocalTime.ofSecondOfDay(86399L) is :-
23:59:59
Exception in thread "main" java.time.DateTimeException:
Invalid value for SecondOfDay (valid values 0 - 86399): 86499
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.ofSecondOfDay(LocalTime.java:382)
at in.bench.resources.java8.localtime.examples.ConvertSecondsToLocalTime
.main(ConvertSecondsToLocalTime.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 !!