The withSecond() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the seconds changed to the seconds passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.
public LocalDateTime withSecond(int seconds)
Parameter: This method accepts a single mandatory parameter seconds which specifies the seconds to be set in the resultant LocalDateTime instance. The value of this seconds can range from 0 to 59.
Returns: The function returns a LocalDateTime instance with the seconds changed to the seconds passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.
Exceptions: The function throws a DateTimeException if the seconds value is invalid.
Below programs illustrate the LocalDateTime.withSecond() method:
Original LocalDateTime: 2018-11-30T10:39:52.924 New LocalDateTime: 2018-11-30T10:39:00.924
Original LocalDateTime: 2015-04-06T10:15:30 New LocalDateTime: 2015-04-06T10:15:59
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.