Java 8 – How to add Date and Time fields to ZonedDateTime ?

In this article, we will learn how to add Date (day/week/month/year) & Time (nano/second/minute/hour) fields to ZonedDateTime using different methods provided in the Java 1.8 version

Adding Date & Time fields to ZonedDateTime :

  • Adding Date & Time fields to ZonedDateTime is quite simple using different methods provided
  • Use below methods to add Day or Week or Month or Year fields to ZonedDateTime
    1. plusDays() – Returns a copy of invoking ZonedDateTime with the specified number of days added
    2. plusWeeks() – Returns a copy of invoking ZonedDateTime with the specified number of weeks added
    3. plusMonths() – Returns a copy of invoking ZonedDateTime with the specified number of months added
    4. plusYears() – Returns a copy of invoking ZonedDateTime with the specified number of years added
  • Likewise, use below methods to add Nanosecond or Second or Minute or Hour fields to ZonedDateTime
    1. plusNanos() – Returns a copy of invoking ZonedDateTime with the specified number of nanoseconds added
    2. plusSeconds() – Returns a copy of invoking ZonedDateTime with the specified number of seconds added
    3. plusMinutes() – Returns a copy of invoking ZonedDateTime with the specified number of minutes added
    4. plusHours() – Returns a copy of invoking ZonedDateTime with the specified number of hours added
  • In the below illustration, we are going to do below operations with default ZonedDateTime,
    1. Add 5 Days to ZonedDateTime using plusDays() method
    2. Add 2 Weeks to ZonedDateTime using plusWeeks() method
    3. Add 3 Months to ZonedDateTime using plusMonths() method
    4. Add 1 Year to ZonedDateTime using plusYears() method
    5. Add 125 Nanos to ZonedDateTime using plusNanos() method
    6. Add 37 Seconds to ZonedDateTime using plusSeconds() method
    7. Add 19 Minutes to ZonedDateTime using plusMinutes() method
    8. Add 5 Hours to ZonedDateTime using plusHours() method
  • Finally, print ZonedDateTime after each operation to the console

AddToZonedDateTime.java

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

import java.time.ZonedDateTime;

public class AddToZonedDateTime {

	public static void main(String[] args) {

		// get Zoned date/time
		ZonedDateTime zonedDateTime = ZonedDateTime.now();
		System.out.println("Zoned Date/time is :- " + zonedDateTime);


		// 1. Adding Day/Week/Month/Year to ZonedDateTime
		System.out.println("\nAdding Day/Week/Month/Year to ZonedDateTime :-\n");


		// 1.1 add 5 days with Zoned date
		ZonedDateTime add_5_Days = zonedDateTime.plusDays(5);
		System.out.println("1. After adding 5 Days to Zoned Date/time is :- "
				+ add_5_Days);


		// 1.2 add 2 weeks to Zoned date
		ZonedDateTime add_2_Weeks = zonedDateTime.plusWeeks(2);
		System.out.println("2. After adding 2 Weeks to Zoned Date/time is :- "
				+ add_2_Weeks);


		// 1.3 add 3 months to Zoned date
		ZonedDateTime add_3_Months = zonedDateTime.plusMonths(3);
		System.out.println("3. After adding 3 Months to Zoned Date/time is :- "
				+ add_3_Months);


		// 1.4 add 1 year to Zoned date
		ZonedDateTime add_1_Year = zonedDateTime.plusYears(1);
		System.out.println("4. After adding 1 Year to Zoned Date/time is :- "
				+ add_1_Year);



		// 2. Adding Nano/Second/Minute/Hour to ZonedDateTime
		System.out.println("\nAdding Nano/Second/Minute/Hour to ZonedDateTime :-\n");


		// 2.1 add 125 NanoSeconds to Zoned time
		ZonedDateTime add_125_Nanos = zonedDateTime.plusNanos(125);
		System.out.println("1. After adding 125 Nano Seconds to Zoned Date/time is - "
				+ add_125_Nanos);


		// 2.2 add 37 Seconds to Zoned time
		ZonedDateTime add_37_Seconds = zonedDateTime.plusSeconds(37);
		System.out.println("2. After adding 37 Seconds to Zoned Date/time is - "
				+ add_37_Seconds);


		// 2.3 add 19 Minutes to Zoned time
		ZonedDateTime add_19_Minutes = zonedDateTime.plusMinutes(19);
		System.out.println("3. After adding 19 Minutes to Zoned Date/time is - "
				+ add_19_Minutes);


		// 2.4 add 5 Hours to Zoned time
		ZonedDateTime add_5_Hours = zonedDateTime.plusHours(5);
		System.out.print("4. After adding 5 Hours to Zoned Date/time is - "
				+ add_5_Hours);
	}
}

Output:

Zoned Date/time is :- 2022-08-14T19:40:28.109941800+05:30[Asia/Calcutta]

Adding Day/Week/Month/Year to ZonedDateTime :-

1. After adding 5 Days to Zoned Date/time is :- 2022-08-19T19:40:28.109941800+05:30[Asia/Calcutta]
2. After adding 2 Weeks to Zoned Date/time is :- 2022-08-28T19:40:28.109941800+05:30[Asia/Calcutta]
3. After adding 3 Months to Zoned Date/time is :- 2022-11-14T19:40:28.109941800+05:30[Asia/Calcutta]
4. After adding 1 Year to Zoned Date/time is :- 2023-08-14T19:40:28.109941800+05:30[Asia/Calcutta]

Adding Nano/Second/Minute/Hour to ZonedDateTime :-

1. After adding 125 Nano Seconds to Zoned Date/time is- 2022-08-14T19:40:28.109941925+05:30[Asia/Calcutta]
2. After adding 37 Seconds to Zoned Date/time is - 2022-08-14T19:41:05.109941800+05:30[Asia/Calcutta]
3. After adding 19 Minutes to Zoned Date/time is - 2022-08-14T19:59:28.109941800+05:30[Asia/Calcutta]
4. After adding 5 Hours to Zoned Date/time is - 2022-08-15T00:40:28.109941800+05:30[Asia/Calcutta]

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – How to subtract Date and Time fields from ZonedDateTime ?
Java 8 – How to convert java.util.Date to ZonedDateTime in different ways ?