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

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

Adding Date & Time fields to OffsetDateTime :

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

AddToOffsetDateTime.java

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

import java.time.OffsetDateTime;

public class AddToOffsetDateTime {

	public static void main(String[] args) {

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


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


		// 1.1 add 5 days with Offset date
		OffsetDateTime add_5_Days = offsetDateTime.plusDays(5);
		System.out.println("1. After adding 5 Days to Offset Date/time is = "
				+ add_5_Days);


		// 1.2 add 2 weeks to Offset date
		OffsetDateTime add_2_Weeks = offsetDateTime.plusWeeks(2);
		System.out.println("2. After adding 2 Weeks to Offset Date/time is = "
				+ add_2_Weeks);


		// 1.3 add 3 months to Offset date
		OffsetDateTime add_3_Months = offsetDateTime.plusMonths(3);
		System.out.println("3. After adding 3 Months to Offset Date/time is = "
				+ add_3_Months);


		// 1.4 add 1 year to Offset date
		OffsetDateTime add_1_Year = offsetDateTime.plusYears(1);
		System.out.println("4. After adding 1 Year to Offset Date/time is = "
				+ add_1_Year);



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


		// 2.1 add 125 NanoSeconds to Offset time
		OffsetDateTime add_125_Nanos = offsetDateTime.plusNanos(125);
		System.out.println("1. After adding 125 Nanoseconds to Offset Date/time is = "
				+ add_125_Nanos);


		// 2.2 add 37 Seconds to Offset time
		OffsetDateTime add_37_Seconds = offsetDateTime.plusSeconds(37);
		System.out.println("2. After adding 37 Seconds to Offset Date/time is = "
				+ add_37_Seconds);


		// 2.3 add 19 Minutes to Offset time
		OffsetDateTime add_19_Minutes = offsetDateTime.plusMinutes(19);
		System.out.println("3. After adding 19 Minutes to Offset Date/time is = "
				+ add_19_Minutes);


		// 2.4 add 5 Hours to Offset time
		OffsetDateTime add_5_Hours = offsetDateTime.plusHours(5);
		System.out.print("4. After adding 5 Hours to Offset Date/time is = "
				+ add_5_Hours);
	}
}

Output:

Offset Date/time is = 2022-08-17T19:09:47.054626+05:30

Adding Day/Week/Month/Year to OffsetDateTime := 

1. After adding 5 Days to Offset Date/time is = 2022-08-22T19:09:47.054626+05:30
2. After adding 2 Weeks to Offset Date/time is = 2022-08-31T19:09:47.054626+05:30
3. After adding 3 Months to Offset Date/time is = 2022-11-17T19:09:47.054626+05:30
4. After adding 1 Year to Offset Date/time is = 2023-08-17T19:09:47.054626+05:30

Adding Nano/Second/Minute/Hour to OffsetDateTime := 

1. After adding 125 Nanoseconds to Offset Date/time is = 2022-08-17T19:09:47.054626125+05:30
2. After adding 37 Seconds to Offset Date/time is = 2022-08-17T19:10:24.054626+05:30
3. After adding 19 Minutes to Offset Date/time is = 2022-08-17T19:28:47.054626+05:30
4. After adding 5 Hours to Offset Date/time is = 2022-08-18T00:09:47.054626+05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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