LocalDateTime withMonth() method in Java with Examples
The withMonth() method of LocalDateTime class in Java is used to get a copy of this LocalDateTime with the months changed to the months passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.
Syntax:
public LocalDateTime withMonth(int months)
Parameter: This method accepts a single mandatory parameter months which specifies the months to be set in the resultant LocalDateTime instance. The value of this months can range from 1 to 12.
Returns: The function returns a LocalDateTime instance with the months changed to the months passed as the parameter to this method. The remaining values of this LocalDateTime remains the same.
Exceptions: The function throws a DateTimeException if the months value is invalid.
Below programs illustrate the LocalDateTime.withMonth() method:
Program 1:
// Program to illustrate the withMonth() 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 months 5 System.out.println("New LocalDateTime: " + dt.withMonth(5)); } } |
Original LocalDateTime: 2018-11-30T12:51:39.472 New LocalDateTime: 2018-05-30T12:51:39.472
Program 2:
// Program to illustrate the withMonth() 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 months 12 System.out.println("New LocalDateTime: " + dt.withMonth(12)); } } |
Original LocalDateTime: 2015-04-06T10:15:30 New LocalDateTime: 2015-12-06T10:15:30
Reference: https://docs.oracle.com/javase/10/docs/api/java/time/LocalDateTime.html#withMonth(int)
Recommended Posts:
- OffsetDateTime withMonth() method in Java with examples
- LocalDate withMonth() method in Java with Examples
- ZonedDateTime withMonth() method in Java with Examples
- YearMonth withMonth() method in Java with Examples
- MonthDay withMonth() Method in Java with Examples
- LocalDateTime from() method in Java with Examples
- LocalDateTime now() Method in Java with Examples
- LocalDateTime with() Method in Java with Examples
- LocalDateTime until() Method in Java with Examples
- LocalDateTime plus() method in Java with Examples
- LocalDateTime withYear() method in Java with Examples
- LocalDateTime range() method in Java with Examples
- LocalDateTime getDayOfYear() method in Java with Examples
- LocalDateTime toLocalDate() method in Java with Examples
- LocalDateTime getHour() 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.


