LocalDateTime withSecond() method in Java with Examples
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.
Syntax:
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:
Program 1:
// Program to illustrate the withSecond() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Get the LocalDateTime instance LocalDateTime dt = LocalDateTime.now(); // Get the String representation of this LocalDateTime System.out.println("Original LocalDateTime: " + dt.toString()); // Get a new LocalDateTime with seconds 0 System.out.println("New LocalDateTime: " + dt.withSecond(0)); } } |
Original LocalDateTime: 2018-11-30T10:39:52.924 New LocalDateTime: 2018-11-30T10:39:00.924
Program 2:
// Program to illustrate the withSecond() method import java.util.*; import java.time.*; public class GfG { public static void main(String[] args) { // Get the LocalDateTime instance LocalDateTime dt = LocalDateTime .parse("2015-04-06T10:15:30"); // Get the String representation of this LocalDateTime System.out.println("Original LocalDateTime: " + dt.toString()); // Get a new LocalDateTime with seconds 59 System.out.println("New LocalDateTime: " + dt.withSecond(59)); } } |
Original LocalDateTime: 2015-04-06T10:15:30 New LocalDateTime: 2015-04-06T10:15:59
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#withSecond(int)
Recommended Posts:
- OffsetDateTime withSecond() method in Java with examples
- LocalTime withSecond() method in Java with Examples
- ZonedDateTime withSecond() method in Java with Examples
- LocalDateTime until() Method in Java with Examples
- LocalDateTime plus() method in Java with Examples
- LocalDateTime from() method in Java with Examples
- LocalDateTime with() Method in Java with Examples
- LocalDateTime now() Method in Java with Examples
- LocalDateTime plusDays() method in Java with Examples
- LocalDateTime plusMonths() method in Java with Examples
- LocalDateTime plusSeconds() method in Java with Examples
- LocalDateTime plusNanos() method in Java with Examples
- LocalDateTime plusHours() method in Java with Examples
- LocalDateTime plusYears() method in Java with Examples
- LocalDateTime range() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


