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

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,

  1. toSecondOfDay() – This method converts LocalTime into Seconds or seconds of day ranging from 0 seconds to 86399 seconds
  2. 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 * 601 (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:
    1. 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
    2. If the given seconds is exactly 0 then this method will return 00:00
    3. If the given seconds is exactly 86399 then this method will return 23:59:59
    4. 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:

References:

Happy Coding !!
Happy Learning !!

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