Java 8 – How to subtract Date and Time fields from OffsetDateTime ?

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

Subtracting Date & Time fields from OffsetDateTime :

  • Subtracting Date & Time fields from OffsetDateTime is quite simple using different methods provided
  • Use below methods to subtract Day or Week or Month or Year fields from OffsetDateTime
    1. minusDays() – Returns a copy of this OffsetDateTime with the specified number of days subtracted
    2. minusWeeks() – Returns a copy of this OffsetDateTime with the specified number of weeks subtracted
    3. minusMonths() – Returns a copy of this OffsetDateTime with the specified number of months subtracted
    4. minusYears() – Returns a copy of this OffsetDateTime with the specified number of years subtracted
  • Likewise, use below methods to subtract Nanosecond or Second or Minute or Hour fields from OffsetDateTime
    1. minusNanos() – Returns a copy of this OffsetDateTime with the specified number of nanoseconds subtracted
    2. minusSeconds() – Returns a copy of this OffsetDateTime with the specified number of seconds subtracted
    3. minusMinutes() – Returns a copy of this OffsetDateTime with the specified number of minutes subtracted
    4. minusHours() – Returns a copy of this OffsetDateTime with the specified number of hours subtracted
  • In the below illustration, we are going to do below operations with default OffsetDateTime,
    1. Subtract 5 Days from OffsetDateTime using minusDays() method
    2. Subtract 2 Weeks from OffsetDateTime using minusWeeks() method
    3. Subtract 3 Months from OffsetDateTime using minusMonths() method
    4. Subtract 1 Year from OffsetDateTime using minusYears() method
    5. Subtract 125 Nanos from OffsetDateTime using minusNanos() method
    6. Subtract 37 Seconds from OffsetDateTime using minusSeconds() method
    7. Subtract 19 Minutes from OffsetDateTime using minusMinutes() method
    8. Subtract 5 Hours from OffsetDateTime using minusHours() method
  • Finally, print OffsetDateTime after each operation to the console

SubtractFromOffsetDateTime.java

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

import java.time.OffsetDateTime;

public class SubtractFromOffsetDateTime {

	public static void main(String[] args) {

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


		// 1. Subtracting Day/Week/Month/Year to OffsetDateTime
		System.out.println("\nSubtracting Day/Week/Month/Year from OffsetDateTime :- \n");


		// 1.1 subtract 5 days
		OffsetDateTime subtract_5_Days = offsetDateTime.minusDays(5);
		System.out.println("1. After subtracting 5 Days is = " 
				+ subtract_5_Days);


		// 1.2 subtract 2 weeks
		OffsetDateTime subtract_2_Weeks = offsetDateTime.minusWeeks(2);
		System.out.println("2. After subtracting 2 Weeks is = " 
				+ subtract_2_Weeks);


		// 1.3 subtract 3 months
		OffsetDateTime subtract_3_Months = offsetDateTime.minusMonths(3);
		System.out.println("3. After subtracting 3 Months is = " 
				+ subtract_3_Months);


		// 1.4 subtract 1 year
		OffsetDateTime subtract_1_Year = offsetDateTime.minusYears(1);
		System.out.println("4. After subtracting 1 Year is = " 
				+ subtract_1_Year);



		// 2. Subtracting Nano/Second/Minute/Hour from OffsetDateTime
		System.out.println("\nSubtracting Nano/Second/Minute/Hour from OffsetDateTime :- \n");


		// 2.1 subtract 125 NanoSeconds
		OffsetDateTime sub_125_Nanos = offsetDateTime.minusNanos(125);
		System.out.println("1. After subtracting 125 Nanoseconds is = " 
				+ sub_125_Nanos);


		// 2.2 subtract 37 Seconds
		OffsetDateTime sub_37_Seconds = offsetDateTime.minusSeconds(37);
		System.out.println("2. After subtracting 37 Seconds is = " 
				+ sub_37_Seconds);


		// 2.3 subtract 19 Minutes
		OffsetDateTime sub_19_Minutes = offsetDateTime.minusMinutes(19);
		System.out.println("3. After subtracting 19 Minutes is = " 
				+ sub_19_Minutes);


		// 2.4 subtract 5 Hours
		OffsetDateTime sub_5_Hours = offsetDateTime.minusHours(5);
		System.out.print("4. After subtracting 5 Hours is = " 
				+ sub_5_Hours);
	}
}

Output:

Offset Date/time is = 2022-08-17T19:19:04.462634200+05:30

Subtracting Day/Week/Month/Year from OffsetDateTime :- 

1. After subtracting 5 Days is = 2022-08-12T19:19:04.462634200+05:30
2. After subtracting 2 Weeks is = 2022-08-03T19:19:04.462634200+05:30
3. After subtracting 3 Months is = 2022-05-17T19:19:04.462634200+05:30
4. After subtracting 1 Year is = 2021-08-17T19:19:04.462634200+05:30

Subtracting Nano/Second/Minute/Hour from OffsetDateTime :- 

1. After subtracting 125 Nanoseconds is = 2022-08-17T19:19:04.462634075+05:30
2. After subtracting 37 Seconds is = 2022-08-17T19:18:27.462634200+05:30
3. After subtracting 19 Minutes is = 2022-08-17T19:00:04.462634200+05:30
4. After subtracting 5 Hours is = 2022-08-17T14:19:04.462634200+05:30

Related Articles:

References:

Happy Coding !!
Happy Learning !!

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