Java 8 – How to alter Date, Time and Zone fields of ZonedDateTime ?

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
    1. withDayOfMonth() – Returns a copy of invoking ZonedDateTime with the day-of-month altered
    2. withMonth() – Returns a copy of invoking ZonedDateTime with the month-of-year altered
    3. withYear() – Returns a copy of invoking ZonedDateTime with the year altered
  • Likewise, use below methods to alter/change/modify Nanosecond or Second or Minute or Hour fields of ZonedDateTime
    1. withHour() – Returns a copy of invoking ZonedDateTime with the hour-of-day altered
    2. withMinute() – Returns a copy of invoking ZonedDateTime with the minute-of-hour altered
    3. withSecond() – Returns a copy of invoking ZonedDateTime with the second-of-minute altered
    4. withNano() – Returns a copy of invoking ZonedDateTime with the nano-of-second altered
  • 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
  • In the below illustration, we are going to do below operations with default ZonedDateTime,
    1. Alter/change/modify Day field of current ZonedDateTime to 15 using withDayOfMonth() method
    2. Alter/change/modify Month field of current ZonedDateTime to 9 using withMonth() method
    3. Alter/change/modify Year field of current ZonedDateTime to 2023 using withYear() method
    4. Alter/change/replace Nano field of current ZonedDateTime to 125 using withNano() method
    5. Alter/change/replace Second field of current ZonedDateTime to 47 using withSecond() method
    6. Alter/change/replace Minute field of current ZonedDateTime to 19 using withMinute() method
    7. Alter/change/replace Hour field of current ZonedDateTime to 5 using withHour() method
    8. Alter/change/replace Zone field of current ZonedDateTime to Europe/Paris using withZoneSameInstant() method retaining current Instant at UTC/GMT
    9. Alter/change/replace Zone field of current ZonedDateTime to Europe/Paris using withZoneSameLocal() method retaining local system date/time
  • 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:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to check whether a ZonedDateTime is Before another ZonedDateTime ?
Java 8 – How to subtract Date and Time fields from ZonedDateTime ?