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
- minusDays() – Returns a copy of this
OffsetDateTime
with the specified number of days subtracted - minusWeeks() – Returns a copy of this
OffsetDateTime
with the specified number of weeks subtracted - minusMonths() – Returns a copy of this
OffsetDateTime
with the specified number of months subtracted - minusYears() – Returns a copy of this
OffsetDateTime
with the specified number of years subtracted
- minusDays() – Returns a copy of this
- Likewise, use below methods to subtract Nanosecond or Second or Minute or Hour fields from OffsetDateTime
- minusNanos() – Returns a copy of this
OffsetDateTime
with the specified number of nanoseconds subtracted - minusSeconds() – Returns a copy of this
OffsetDateTime
with the specified number of seconds subtracted - minusMinutes() – Returns a copy of this
OffsetDateTime
with the specified number of minutes subtracted - minusHours() – Returns a copy of this
OffsetDateTime
with the specified number of hours subtracted
- minusNanos() – Returns a copy of this
- In the below illustration, we are going to do below operations with default OffsetDateTime,
- Subtract 5 Days from OffsetDateTime using minusDays() method
- Subtract 2 Weeks from OffsetDateTime using minusWeeks() method
- Subtract 3 Months from OffsetDateTime using minusMonths() method
- Subtract 1 Year from OffsetDateTime using minusYears() method
- Subtract 125 Nanos from OffsetDateTime using minusNanos() method
- Subtract 37 Seconds from OffsetDateTime using minusSeconds() method
- Subtract 19 Minutes from OffsetDateTime using minusMinutes() method
- 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:
- Java 8 – OffsetDateTime with method details and examples
- Java 8 – How to get Date, Time and Offset fields from OffsetDateTime ?
- Java 8 – How to form OffsetDateTime passing Date, Time and Offset fields ?
- Java 8 – How to form OffsetDateTime passing LocalDate, LocalTime and ZoneOffset ?
- Java 8 – How to form OffsetDateTime passing LocalDateTime and ZoneOffset ?
- Java 8 – How to form OffsetDateTime passing Instant and ZoneId ?
- Java 8 – How to parse OffsetDateTime in String form ?
- Java 8 – How to convert String to OffsetDateTime ?
- Java 8 – How to convert ZonedDateTime to String ?
- Java 8 – How to convert OffsetDateTime in different formats ?
- Java 8 – How to convert OffsetDateTime in different Format Style ?
- Java 8 – How to convert OffsetDateTime to LocalDateTime ?
- Java 8 – How to convert OffsetDateTime to ZonedDateTime ?
- Java 8 – How to convert OffsetDateTime to an Instant ?
- Java 8 – How to extract LocalDate and LocalTime and LocalDateTime from OffsetDateTime ?
- Java 8 – How to extract OffsetTime from OffsetDateTime ?
- Java 8 – How to convert OffsetDateTime to number of Seconds ?
- Java 8 – How to convert OffsetDateTime to java.util.Date and vice-versa ?
- Java 8 – How to convert OffsetDateTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert OffsetDateTime to Calendar and vice-versa ?
- Java 8 – How to convert OffsetDateTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert OffsetDateTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to an OffsetDateTime in different ways ?
- Java 8 – How to add Date and Time fields to OffsetDateTime ?
- Java 8 – How to subtract Date and Time fields from OffsetDateTime ?
- Java 8 – How to alter Date, Time and Offset fields of OffsetDateTime ?
- Java 8 – How to check whether an OffsetDateTime is Before another OffsetDateTime ?
- Java 8 – How to check whether an OffsetDateTime is After another OffsetDateTime ?
- Java 8 – How to compare two OffsetDateTime instances ?
- Java 8 – How to find difference between two OffsetDateTime using Period & Duration ?
- Java 9 – Find difference between two OffsetDateTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
Happy Coding !!
Happy Learning !!