LocalTime ofSecondOfDay() method in Java

Java 8Object Oriented ProgrammingProgramming

A LocalTime object can be obtained using the seconds of the day with the ofSecondOfDay() method in the LocalTime class in Java. This method requires a single parameter i.e. the seconds of the day and it returns the LocalTime object for the seconds of the day.

A program that demonstrates this is given as follows

Example

 Live Demo

import java.time.*;
public class Demo {
   public static void main(String[] args) {
      long seconds = 18930;
      System.out.println("The seconds of the day: " + seconds);
      System.out.println("The LocalTime is: " + LocalTime.ofSecondOfDay(seconds));
   }
}

output

The seconds of the day: 18930
The LocalTime is: 05:15:30

Now let us understand the above program.

First the seconds of the day are displayed. Then the LocalTime object is obtained using the seconds of the day with the ofSecondOfDay() method and printed. A code snippet that demonstrates this is as follows:

long seconds = 18930;
System.out.println("The seconds of the day: " + seconds);
System.out.println("The LocalTime is: " + LocalTime.ofSecondOfDay(seconds));
raja
Updated on 30-Jul-2019 22:30:25

Advertisements