Java 8 – How to alter Date, Time and Offset fields of OffsetDateTime ?

In this article, we will learn how to alter/modify/change Date (day/month/year) & Time (nano/second/minute/hour) & Offset fields of OffsetDateTime using different methods provided in the Java 1.8 version

Altering Date/Time/Offset fields of OffsetDateTime :

  • Altering/modifying Date & Time & Offset fields of OffsetDateTime is quite simple using different methods provided
  • Use below methods to alter/change/modify Day or Month or Year fields of OffsetDateTime
    1. withDayOfMonth() – Returns a copy of this OffsetDateTime with the day-of-month altered
    2. withMonth() – Returns a copy of this OffsetDateTime with the month-of-year altered
    3. withYear() – Returns a copy of this OffsetDateTime with the year altered
  • Likewise, use below methods to alter/change/modify Nanosecond or Second or Minute or Hour fields of OffsetDateTime
    1. withHour() – Returns a copy of this OffsetDateTime with the hour-of-day altered
    2. withMinute() – Returns a copy of this OffsetDateTime with the minute-of-hour altered
    3. withSecond() – Returns a copy of this OffsetDateTime with the second-of-minute altered
    4. withNano() – Returns a copy of this OffsetDateTime 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,
    • withOffsetSameInstant() – Returns a copy of this OffsetDateTime with the specified offset ensuring that the result is at the same instant
    • withOffsetSameLocal() – Returns a copy of this OffsetDateTime with the specified offset ensuring that the result has the same local date-time
  • In the below illustration, we are going to do below operations with default OffsetDateTime,
    1. Alter/change/modify Day field of current OffsetDateTime to 15 using withDayOfMonth() method
    2. Alter/change/modify Month field of current OffsetDateTime to 9 using withMonth() method
    3. Alter/change/modify Year field of current OffsetDateTime to 2023 using withYear() method
    4. Alter/change/replace Nano field of current OffsetDateTime to 125 using withNano() method
    5. Alter/change/replace Second field of current OffsetDateTime to 47 using withSecond() method
    6. Alter/change/replace Minute field of current OffsetDateTime to 19 using withMinute() method
    7. Alter/change/replace Hour field of current OffsetDateTime to 5 using withHour() method
    8. Alter/change/replace Offset field of current OffsetDateTime to +02:00 using withZoneSameInstant() method retaining date/time at specified Offset Instant
    9. Alter/change/replace Offset field of current OffsetDateTime to +02:00 using withZoneSameLocal() method retaining local system date/time
  • Finally, print OffsetDateTime after each operation to the console

AlterOffsetDateTime.java

package in.bench.resources.java8.offsetdatetime.examples;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class AlterOffsetDateTime {

	public static void main(String[] args) {

		// get Offset System Date/time
		OffsetDateTime offsetDateTime = OffsetDateTime.now();
		System.out.println("Offset Date/time in ISO_Offset_DATE_TIME format is = "
				+ offsetDateTime);


		// 1. Altering Day/Month/Year parts of OffsetDateTime
		System.out.println("\nAltering Day/Month/Year parts of OffsetDateTime :- \n");


		// 1.1 alter/change/modify DAY part of Offset System Date/time
		OffsetDateTime dateAltered = offsetDateTime.withDayOfMonth(15);
		System.out.println("1. Day (15) altered in Offset Date/time is = "
				+ dateAltered);


		// 1.2 alter/change/modify MONTH part of Offset System Date/time
		OffsetDateTime monthAltered = offsetDateTime.withMonth(9);
		System.out.println("2. Month (9) altered in Offset Date/time is = "
				+ monthAltered);


		// 1.3 alter/change/modify YEAR part of Offset System Date/time
		OffsetDateTime yearAltered = offsetDateTime.withYear(2023);
		System.out.println("3. Year (2023) altered in Offset Date/time is = "
				+ yearAltered);



		// 2. Altering Nano/Second/Minute/Hour of OffsetDateTime
		System.out.println("\nAltering Nano/Second/Minute/Hour parts of OffsetDateTime :- \n");


		// 2.1 alter/change/modify HOUR part to Offset System Date/time
		OffsetDateTime hourAltered = offsetDateTime.withHour(5);
		System.out.println("1. Hours (5) altered in Offset Date/time is = " 
				+ hourAltered);


		// 2.2 alter/change/modify MINUTE part to Offset system Date/time
		OffsetDateTime minuteAltered = offsetDateTime.withMinute(19); 
		System.out.println("2. Minutes (19) altered in Offset Date/time is = " 
				+ minuteAltered);


		// 2.3 alter/change/modify SECOND part to Offset system Date/time
		OffsetDateTime secondAltered = offsetDateTime.withSecond(47);
		System.out.println("3. Seconds (47) altered in Offset Date/time is = " 
				+ secondAltered);


		// 2.4 alter/change/modify NANOSECOND part to Offset system Date/time
		OffsetDateTime nanoAltered = offsetDateTime.withNano(125);
		System.out.println("4. Nanoseconds (125) altered in Offset Date/time is = "
				+ nanoAltered);



		// 3. Altering Offset of OffsetDateTime
		System.out.println("\nAltering Zone of OffsetDateTime :- \n");

		ZoneOffset zoneOffset = ZoneOffset.of("+02:00");
		System.out.println("ZoneOffset is = " + zoneOffset);


		// 3.1 alter/change/modify ZONE part to Offset system Date/time
		OffsetDateTime offsetAltered = offsetDateTime.withOffsetSameInstant(zoneOffset);
		System.out.println("1. Offset (+02:00) altered in Offset Date/time is = " 
				+ offsetAltered);


		// 3.2 alter/change/modify ZONE part to Offset system Date/time
		OffsetDateTime offsetAlteredLocal = offsetDateTime.withOffsetSameLocal(zoneOffset);
		System.out.print("2. Offset (+02:00) altered in Offset Date/time is = " 
				+ offsetAlteredLocal);
	}
}

Output:

Offset Date/time in ISO_Offset_DATE_TIME format is = 2022-08-17T19:28:26.864041500+05:30

Altering Day/Month/Year parts of OffsetDateTime :- 

1. Day (15) altered in Offset Date/time is = 2022-08-15T19:28:26.864041500+05:30
2. Month (9) altered in Offset Date/time is = 2022-09-17T19:28:26.864041500+05:30
3. Year (2023) altered in Offset Date/time is = 2023-08-17T19:28:26.864041500+05:30

Altering Nano/Second/Minute/Hour parts of OffsetDateTime :- 

1. Hours (5) altered in Offset Date/time is = 2022-08-17T05:28:26.864041500+05:30
2. Minutes (19) altered in Offset Date/time is = 2022-08-17T19:19:26.864041500+05:30
3. Seconds (47) altered in Offset Date/time is = 2022-08-17T19:28:47.864041500+05:30
4. Nanoseconds (125) altered in Offset Date/time is = 2022-08-17T19:28:26.000000125+05:30

Altering Zone of OffsetDateTime :- 

ZoneOffset is = +02:00
1. Offset (+02:00) altered in Offset Date/time is = 2022-08-17T15:58:26.864041500+02:00
2. Offset (+02:00) altered in Offset Date/time is = 2022-08-17T19:28:26.864041500+02:00

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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