In this article, we will learn how to alter/modify/change Date (day/month/year) & Time (nano/second/minute/hour) fields of LocalDateTime using different methods provided in the Java 1.8 version
Altering Date & Time fields of LocalDateTime :
- Altering/modifying Date & Time fields of LocalDateTime is quite simple using different methods provided
- Use below methods to alter/change/modify Day or Month or Year fields of LocalDateTime
- withDayOfMonth() – Returns a copy of this
with the day-of-month alteredLocalDateTime
- withMonth() – Returns a copy of this
with the month-of-year alteredLocalDateTime
- withYear() – Returns a copy of this
with the year alteredLocalDateTime
- withDayOfMonth() – Returns a copy of this
- Likewise, use below methods to alter/change/modify Nanosecond or Second or Minute or Hour fields of LocalDateTime
- withNano() – Returns a copy of this
with the nano-of-second alteredLocalDateTime
- withSecond() – Returns a copy of this
with the second-of-minute alteredLocalDateTime
- withMinute() – Returns a copy of this
with the minute-of-hour alteredLocalDateTime
- withHour() – Returns a copy of this
with the hour-of-day alteredLocalDateTime
- withNano() – Returns a copy of this
- In the below illustration, we are going to do below operations with current LocalDateTime,
- Alter/change/modify Day field of current
to 15 using withDayOfMonth() methodLocalDateTime
- Alter/change/modify Month field of current
to 9 using withMonth() methodLocalDateTime
- Alter/change/modify Year field of current
to 2023 using withYear() methodLocalDateTime
- Alter/change/replace Nano field of current
to 125 using withNano() methodLocalDateTime
- Alter/change/replace Second field of current
to 47 using withSecond() methodLocalDateTime
- Alter/change/replace Minute field of current
to 19 using withMinute() methodLocalDateTime
- Alter/change/replace Hour field of current
to 5 using withHour() methodLocalDateTime
- Alter/change/modify Day field of current
- Finally, print LocalDateTime after each operation to the console
AlterLocalDateTime.java
package in.bench.resources.java8.localdatetime.examples;
import java.time.LocalDateTime;
public class AlterLocalDateTime {
public static void main(String[] args) {
// get Current System Date/time
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("Current Date/time in ISO_LOCAL_DATE_TIME format is = "
+ localDateTime);
// 1. Altering Day/Month/Year parts of LocalDateTime
System.out.println("\n\nAltering Day/Month/Year parts of LocalDateTime :- \n");
// 1.1 alter/change/modify DAY part of Current System Date/time
LocalDateTime dateAltered = localDateTime.withDayOfMonth(15);
System.out.println("1. Day (15) altered in Current System Date/time is = "
+ dateAltered);
// 1.2 alter/change/modify MONTH part of Current System Date/time
LocalDateTime monthAltered = localDateTime.withMonth(9);
System.out.println("2. Month (9) altered in Current System Date/time is = "
+ monthAltered);
// 1.3 alter/change/modify YEAR part of Current System Date/time
LocalDateTime yearAltered = localDateTime.withYear(2023);
System.out.println("3. Year (2023) altered in Current System Date/time is = "
+ yearAltered);
// 2. Altering Nano/Second/Minute/Hour from LocalDateTime
System.out.println("\n\nAltering Nano/Second/Minute/Hour parts of LocalDateTime :- \n");
// 2.1 alter/change/modify HOUR part to Current System Date/time
LocalDateTime hourAltered = localDateTime.withHour(5);
System.out.println("1. Hours (5) altered in Current System Date/time is = "
+ hourAltered);
// 2.2 alter/change/modify MINUTE part to current system Date/time
LocalDateTime minuteAltered = localDateTime.withMinute(19);
System.out.println("2. Minutes (19) altered in Current System Date/time is = "
+ minuteAltered);
// 2.3 alter/change/modify SECOND part to current system Date/time
LocalDateTime secondAltered = localDateTime.withSecond(47);
System.out.println("3. Seconds (47) altered in Current System Date/time is = "
+ secondAltered);
// 2.4 alter/change/modify NANOSECOND part to current system Date/time
LocalDateTime nanoAltered = localDateTime.withNano(125);
System.out.print("4. Nanoseconds (125) altered in Current System Date/time is = "
+ nanoAltered);
}
}
Output:
Current Date/time in ISO_LOCAL_DATE_TIME format is = 2022-08-10T18:40:24.397501300
Altering Day/Month/Year parts of LocalDateTime :-
1. Day (15) altered in Current System Date/time is = 2022-08-15T18:40:24.397501300
2. Month (9) altered in Current System Date/time is = 2022-09-10T18:40:24.397501300
3. Year (2023) altered in Current System Date/time is = 2023-08-10T18:40:24.397501300
Altering Nano/Second/Minute/Hour parts of LocalDateTime :-
1. Hours (5) altered in Current System Date/time is = 2022-08-10T05:40:24.397501300
2. Minutes (19) altered in Current System Date/time is = 2022-08-10T18:19:24.397501300
3. Seconds (47) altered in Current System Date/time is = 2022-08-10T18:40:47.397501300
4. Nanoseconds (125) altered in Current System Date/time is = 2022-08-10T18:40:24.000000125
Related Articles:
- Java 8 – LocalDateTime with method details and examples
- Java 8 – How to get Date and Time fields from LocalDateTime ?
- Java 8 – How to form LocalDateTime passing Date and Time fields ?
- Java 8 – How to form LocalDateTime passing LocalDate and LocalTime ?
- Java 8 – How to form LocalDateTime passing Instant and ZoneId ?
- Java 8 – How to form LocalDateTime passing Second/Nano and ZoneOffset ?
- Java 8 – How to parse LocalDateTime in String form ?
- Java 8 – How to convert String to LocalDateTime ?
- Java 8 – How to convert LocalDateTime to String ?
- Java 8 – How to convert LocalDateTime in different formats ?
- Java 8 – How to convert LocalDateTime in different Format Style ?
- Java 8 – How to convert LocalDateTime to ZonedDateTime ?
- Java 8 – How to convert LocalDateTime to an OffsetDateTime ?
- Java 8 – How to convert LocalDateTime to an Instant ?
- Java 8 – How to extract LocalDateTime and LocalTime from LocalDateTime ?
- Java 8 – How to convert LocalDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalDateTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalDateTime in different ways ?
- Java 8 – How to add Date and Time fields to LocalDateTime ?
- Java 8 – How to subtract Date and Time fields from LocalDateTime ?
- Java 8 – How to alter Date and Time fields of LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is Before another LocalDateTime ?
- Java 8 – How to check whether a LocalDateTime is After another LocalDateTime ?
- Java 8 – How to compare two LocalDateTime instances ?
- Java 8 – How to find difference between two LocalDateTime using Period & Duration ?
- Java 8 – What are all the Temporal Fields supported by LocalDateTime ?
- Java 8 – What are all the Temporal Units supported by LocalDateTime ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.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 !!