Friday, June 28, 2024
HomeLanguagesJavaLocalTime withSecond() method in Java with Examples

LocalTime withSecond() method in Java with Examples

The withSecond() method of a LocalTime class is used to get a copy of this LocalTime with the seconds changed to the seconds passed as the parameter to this method. The remaining values of this LocalTime will remain the same. This instance is immutable and unaffected by this method call.

Syntax:

public LocalTime withSecond(int second)

Parameters: This method accepts a single parameter second which represents the second-of-minute to set in the result, from 0 to 59.

Return value: This method returns a LocalTime instance based on this time with the requested second.

Exception: This method throws a exception DateTimeException if the second value is invalid

Below programs illustrate the withSecond() method:

Program 1:




// Java program to demonstrate
// LocalTime.withSecond() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("19:34:50.63");
  
        // print time
        System.out.println("Old LocalTime: "
                           + time);
  
        // Get a new LocalDateTime with seconds 4
        LocalTime newtime = time.withSecond(4);
  
        // print result
        System.out.println("New LocalDateTime: "
                           + newtime);
    }
}


Output:

Old LocalTime: 19:34:50.630
New LocalDateTime: 19:34:04.630

Program 2:




// Java program to demonstrate
// LocalTime.withSecond() method
  
import java.time.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // create a LocalTime object
        LocalTime time
            = LocalTime.parse("01:21:30.13");
  
        // print time
        System.out.println("Old LocalTime: "
                           + time);
  
        // Get a new LocalDateTime with seconds 23
        LocalTime newtime = time.withSecond(23);
  
        // print result
        System.out.println("New LocalDateTime: "
                           + newtime);
    }
}


Output:

Old LocalTime: 01:21:30.130
New LocalDateTime: 01:21:23.130

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

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