In this article, we will learn how to alter Year or Month or DayOfMonth fields of LocalDate using different methods provided in Java 1.8 version
Altering Day/Month/Year fields of LocalDate:
- It is very simple to alter Day or Month or Year fields of LocalDate using below methods,
- withDayOfMonth() – Returns a copy of this
LocalDate
with the day-of-month altered - withMonth() – Returns a copy of this
LocalDate
with the month-of-year altered - withYear() – Returns a copy of this
LocalDate
with the year altered
- withDayOfMonth() – Returns a copy of this
- In the below illustration, we are going to do alter operations with current LocalDate,
- Alter/change Day field of current LocalDate to 15 using withDayOfMonth() method
- Alter/change Month field of current LocalDate to 8 using withMonth() method
- Alter/change Year field of current LocalDate to 2023 using withYear() method
- Finally, print LocalDate to the console
AlterLocalDate.java
package in.bench.resources.java8.localdate.examples;
import java.time.LocalDate;
public class AlterLocalDate {
public static void main(String[] args) {
// 1. get Current System Date
LocalDate currentLocalDate = LocalDate.now();
System.out.println("Current Date in ISO_LOCAL_DATE format is = "
+ currentLocalDate);
// 1.1 alter/change day part of Current System Date
LocalDate dateAltered = currentLocalDate.withDayOfMonth(15);
System.out.println("\n1. Day (15) altered in Current System Date is = "
+ dateAltered);
// 1.2 alter/change Month part of Current System Date
LocalDate monthAltered = currentLocalDate.withMonth(8);
System.out.println("2. Month (8) altered in Current System Date is = "
+ monthAltered);
// 1.3 alter/change Year part of Current System Date
LocalDate yearAltered = currentLocalDate.withYear(2023);
System.out.print("3. Year (2023) altered in Current System Date is = "
+ yearAltered);
}
}
Output:
Current Date in ISO_LOCAL_DATE format is = 2022-07-30
1. Day (15) altered in Current System Date is = 2022-07-15
2. Month (8) altered in Current System Date is = 2022-08-30
3. Year (2023) altered in Current System Date is = 2023-07-30
Related Articles:
- Java 8 – LocalDate with method details and examples
- Java 8 – How to get Year, Month and Day fields from LocalDate ?
- Java 8 – How to get Number of Days in a Month from LocalDate ?
- Java 8 – How to get Number of Days in a Year from LocalDate ?
- Java 8 – How to form LocalDate passing Year, Month and Day fields ?
- Java 8 – How to parse LocalDate in String form ?
- Java 8 – How to convert String to LocalDate ?
- Java 8 – How to convert LocalDate to String ?
- Java 8 – How to convert LocalDate in different formats ?
- Java 8 – How to convert LocalDate in different Format Style ?
- Java 8 – How to convert LocalDate to LocalDateTime ?
- Java 8 – How to convert LocalDate to ZonedDateTime ?
- Java 8 – How to convert LocalDate to an OffsetDateTime ?
- Java 8 – How to convert LocalDate to an Instant ?
- Java 8 – How to convert LocalDate to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.Calendar and vice-versa ?
- Java 8 – How to convert LocalDate to java.util.GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDate to javax.xml.datatype.XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDate in different ways ?
- Java 8 – How to add Year, Month and Day fields to LocalDate ?
- Java 8 – How to subtract Year, Month and Day fields from LocalDate ?
- Java 8 – How to alter Year, Month and Day fields of LocalDate ?
- Java 8 – How to check whether a LocalDate is Before another LocalDate ?
- Java 8 – How to check whether a LocalDate is After another LocalDate ?
- Java 8 – How to compare two LocalDate instances ?
- Java 8 – How to get remaining number of Days in a Year using LocalDate ?
- Java 8 – How to find difference between two LocalDate instances using Period ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!