In this article, we will learn how to alter/modify/change Date (day/month/year) & Time (nano/second/minute/hour) & Zone fields of ZonedDateTime using different methods provided in the Java 1.8 version
Altering Date & Time fields of ZonedDateTime :
- Altering/modifying Date & Time fields of ZonedDateTime is quite simple using different methods provided
- Use below methods to alter/change/modify Day or Month or Year fields of ZonedDateTime
- withDayOfMonth() – Returns a copy of invoking
ZonedDateTime
with the day-of-month altered - withMonth() – Returns a copy of invoking
ZonedDateTime
with the month-of-year altered - withYear() – Returns a copy of invoking
ZonedDateTime
with the year altered
- withDayOfMonth() – Returns a copy of invoking
- Likewise, use below methods to alter/change/modify Nanosecond or Second or Minute or Hour fields of ZonedDateTime
- withHour() – Returns a copy of invoking
ZonedDateTime
with the hour-of-day altered - withMinute() – Returns a copy of invoking
ZonedDateTime
with the minute-of-hour altered - withSecond() – Returns a copy of invoking
ZonedDateTime
with the second-of-minute altered - withNano() – Returns a copy of invoking
ZonedDateTime
with the nano-of-second altered
- withHour() – Returns a copy of invoking
- There are 2 methods available to alter/change/modify zone with same instant or local date/time as listed below,
- withZoneSameInstant() – Returns a copy of invoking
ZonedDateTime
with a different time-zone, retaining the instant - withZoneSameLocal() – Returns a copy of invoking
ZonedDateTime
with a different time-zone, retaining the local date-time if possible
- withZoneSameInstant() – Returns a copy of invoking
- In the below illustration, we are going to do below operations with default ZonedDateTime,
- Alter/change/modify Day field of current
to 15 using withDayOfMonth() methodZonedDateTime
- Alter/change/modify Month field of current
to 9 using withMonth() methodZonedDateTime
- Alter/change/modify Year field of current
to 2023 using withYear() methodZonedDateTime
- Alter/change/replace Nano field of current
to 125 using withNano() methodZonedDateTime
- Alter/change/replace Second field of current
to 47 using withSecond() methodZonedDateTime
- Alter/change/replace Minute field of current
to 19 using withMinute() methodZonedDateTime
- Alter/change/replace Hour field of current
to 5 using withHour() methodZonedDateTime
- Alter/change/replace Zone field of current
to Europe/Paris using withZoneSameInstant() method retaining current Instant at UTC/GMTZonedDateTime
- Alter/change/replace Zone field of current
to Europe/Paris using withZoneSameLocal() method retaining local system date/timeZonedDateTime
- Alter/change/modify Day field of current
- Finally, print ZonedDateTime after each operation to the console
AlterZonedDateTime.java
package in.bench.resources.java8.zoneddatetime.examples;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class AlterZonedDateTime {
public static void main(String[] args) {
// get Zoned System Date/time
ZonedDateTime zonedDateTime = ZonedDateTime.now();
System.out.println("Zoned Date/time in ISO_ZONED_DATE_TIME format is = "
+ zonedDateTime);
// 1. Altering Day/Month/Year parts of ZonedDateTime
System.out.println("\nAltering Day/Month/Year parts of ZonedDateTime :- \n");
// 1.1 alter/change/modify DAY part of Zoned System Date/time
ZonedDateTime dateAltered = zonedDateTime.withDayOfMonth(15);
System.out.println("1. Day (15) altered in Zoned Date/time is = "
+ dateAltered);
// 1.2 alter/change/modify MONTH part of Zoned System Date/time
ZonedDateTime monthAltered = zonedDateTime.withMonth(9);
System.out.println("2. Month (9) altered in Zoned Date/time is = "
+ monthAltered);
// 1.3 alter/change/modify YEAR part of Zoned System Date/time
ZonedDateTime yearAltered = zonedDateTime.withYear(2023);
System.out.println("3. Year (2023) altered in Zoned Date/time is = "
+ yearAltered);
// 2. Altering Nano/Second/Minute/Hour of ZonedDateTime
System.out.println("\nAltering Nano/Second/Minute/Hour parts of ZonedDateTime :- \n");
// 2.1 alter/change/modify HOUR part to Zoned System Date/time
ZonedDateTime hourAltered = zonedDateTime.withHour(5);
System.out.println("1. Hours (5) altered in Zoned Date/time is = "
+ hourAltered);
// 2.2 alter/change/modify MINUTE part to Zoned system Date/time
ZonedDateTime minuteAltered = zonedDateTime.withMinute(19);
System.out.println("2. Minutes (19) altered in Zoned Date/time is = "
+ minuteAltered);
// 2.3 alter/change/modify SECOND part to Zoned system Date/time
ZonedDateTime secondAltered = zonedDateTime.withSecond(47);
System.out.println("3. Seconds (47) altered in Zoned Date/time is = "
+ secondAltered);
// 2.4 alter/change/modify NANOSECOND part to Zoned system Date/time
ZonedDateTime nanoAltered = zonedDateTime.withNano(125);
System.out.println("4. Nanoseconds (125) altered in Zoned Date/time is = "
+ nanoAltered);
// 3. Altering Zone from ZonedDateTime
System.out.println("\nAltering Zone of ZonedDateTime :- \n");
ZoneId zoneId = ZoneId.of("Europe/Paris");
System.out.println("Zone is = " + zoneId);
// 3.1 alter/change/modify ZONE part to Zoned system Date/time
ZonedDateTime zoneAltered = zonedDateTime.withZoneSameInstant(zoneId);
System.out.println("1. Zone (Europe/Paris) altered in Zoned Date/time is = "
+ zoneAltered);
// 3.2 alter/change/modify ZONE part to Zoned system Date/time
ZonedDateTime zoneAlteredLocal = zonedDateTime.withZoneSameLocal(zoneId);
System.out.print("2. Zone (Europe/Paris) altered in Zoned Date/time is = "
+ zoneAlteredLocal);
}
}
Output:
Zoned Date/time in ISO_ZONED_DATE_TIME format is = 2022-08-14T20:13:27.785235200+05:30[Asia/Calcutta]
Altering Day/Month/Year parts of ZonedDateTime :-
1. Day (15) altered in Zoned Date/time is = 2022-08-15T20:13:27.785235200+05:30[Asia/Calcutta]
2. Month (9) altered in Zoned Date/time is = 2022-09-14T20:13:27.785235200+05:30[Asia/Calcutta]
3. Year (2023) altered in Zoned Date/time is = 2023-08-14T20:13:27.785235200+05:30[Asia/Calcutta]
Altering Nano/Second/Minute/Hour parts of ZonedDateTime :-
1. Hours (5) altered in Zoned Date/time is = 2022-08-14T05:13:27.785235200+05:30[Asia/Calcutta]
2. Minutes (19) altered in Zoned Date/time is = 2022-08-14T20:19:27.785235200+05:30[Asia/Calcutta]
3. Seconds (47) altered in Zoned Date/time is = 2022-08-14T20:13:47.785235200+05:30[Asia/Calcutta]
4. Nanoseconds (125) altered in Zoned Date/time is = 2022-08-14T20:13:27.000000125+05:30[Asia/Calcutta]
Altering Zone of ZonedDateTime :-
Zone is = Europe/Paris
1. Zone (Europe/Paris) altered in Zoned Date/time is = 2022-08-14T16:43:27.785235200+02:00[Europe/Paris]
2. Zone (Europe/Paris) altered in Zoned Date/time is = 2022-08-14T20:13:27.785235200+02:00[Europe/Paris]
Related Articles:
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – How to get Date, Time and Zone fields from ZonedDateTime ?
- Java 8 – How to form ZonedDateTime passing Date, Time and Zone fields ?
- Java 8 – How to form ZonedDateTime passing LocalDate, LocalTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing LocalDateTime and ZoneId ?
- Java 8 – How to form ZonedDateTime passing Instant and ZoneId ?
- Java 8 – How to parse ZonedDateTime in String form ?
- Java 8 – How to convert String to ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert ZonedDateTime in different formats ?
- Java 8 – How to convert ZonedDateTime in different Format Style ?
- Java 8 – How to convert ZonedDateTime to LocalDateTime ?
- Java 8 – How to convert ZonedDateTime to an OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from ZonedDateTime ?
- Java 8 – How to convert ZonedDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert ZonedDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert ZonedDateTime to Calendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert ZonedDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?
- Java 8 – How to add Date and Time fields to ZonedDateTime ?
- Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
- Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
- Java 8 – How to check whether a ZonedDateTime is After another ZonedDateTime ?
- Java 8 – How to compare two ZonedDateTime instances ?
- Java 8 – How to find difference between two ZonedDateTime using Period & Duration ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.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 !!