Wednesday, July 3, 2024
HomeLanguagesJavaLocalTime ofSecondOfDay() method in Java with Examples

LocalTime ofSecondOfDay() method in Java with Examples

The ofSecondOfDay() method of LocalTime class is used to obtain LocalTime Instance from a second-of-day value. This returns a LocalTime with the specified the second-of-day, from 0 to 24 * 60 * 60 – 1 passed as parameter.

Syntax:

public static LocalTime ofSecondOfDay(long secondOfDay)

Parameters: This method accepts a single parameter secondOfDay which is the second-of-day, from 0 to 24 * 60 * 60 – 1.

Return value: This method returns LocalTime instance created from the specified secondOfDay.

Exception: This method throws DateTimeException if the second-of-day value is invalid.

Below programs illustrate the ofSecondOfDay() method:

Program 1:




// Java program to demonstrate
// LocalTime.ofSecondOfDay() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a nano of day
        long secondvalue = 14245l;
  
        // applying ofSecondOfDay()
        LocalTime value
            = LocalTime.ofSecondOfDay(secondvalue);
  
        // print result
        System.out.println("LocalTime: "
                           + value);
    }
}


Output:

LocalTime: 03:57:25

Program 2:




// Java program to demonstrate
// LocalTime.ofSecondOfDay() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a nano of day
        long second = 24005;
  
        // applying ofSecondOfDay()
        LocalTime value
            = LocalTime.ofSecondOfDay(second);
  
        // print result
        System.out.println("LocalTime: "
                           + value);
    }
}


Output:

LocalTime: 06:40:05

References: https://docs.oracle.com/javase/10/docs/api/java/time/LocalTime.html#ofSecondOfDay(long)

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments