In this article, we will learn how to find difference between two OffsetDateTime and Calendar/Date instances
1. Find difference between two OffsetDateTime instances :
- To find difference between two OffsetDateTime instances, first we need to segregate Date & Time parts as there is no way to find difference between them directly
- To segregate Date and Time parts separately, we can use toLocalDate() and toLocalTime() methods of OffsetDateTime
- toLocalDate() – gets the
LocalDate
part of invoking OffsetDateTime - toLocalTime() – gets the
LocalTime
part of invoking OffsetDateTime
- toLocalDate() – gets the
- Use Period.between() method to find difference between 2 LocalDate instances
- Period.between() method accepts 2 input-arguments and returns difference of two LocalDate,
- Pass organization joining date as 1st argument
- Pass organization relieving date as 2nd argument
- Period class has many useful method like,
- getYears() – returns numbers of years between 2 LocalDate instances
- getMonths() – returns numbers of months between 2 LocalDate instances
- getDays() – returns numbers of days between 2 LocalDate instances
- Period.between() method accepts 2 input-arguments and returns difference of two LocalDate,
- Similarly, use Duration.between() method to find difference between 2 LocalTime instances
- Duration.between() method accepts 2 input-arguments and returns difference of two LocalTime,
- Pass office start time as 1st argument
- Pass office end time as 2nd argument
- Duration class has many useful methods like,
- getSeconds() – Gets the number of seconds in this duration between 2 LocalTime instances
- getNano() – Gets the number of nanoseconds within the second in this duration between 2 LocalTime instances
- Duration.between() method accepts 2 input-arguments and returns difference of two LocalTime,
- Finally print Time Period/Duration spent in an organization to the console
FindDifferenceOfTwoOffsetDateTime.java
package in.bench.resources.java8.offsetdatetime.examples;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneOffset;
public class FindDifferenceOfTwoOffsetDateTime {
public static void main(String[] args) {
// get default Offset
ZoneOffset zoneOffset = ZoneOffset.of("+05:30");
System.out.println("Default system zone is :- \n" + zoneOffset);
// 1. organization joining date
OffsetDateTime joiningDateTime = OffsetDateTime
.of(2017, 3, 6, 10, 32, 56, 123456789, zoneOffset);
System.out.println("\nOrganization Joining Date/time is :- \n" + joiningDateTime);
// 2. organization relieving date
OffsetDateTime relievingDateTime = OffsetDateTime
.of(2019, 6, 28, 20, 31, 43, 987654321, zoneOffset);
System.out.println("\nOrganization Relieving Date/time is :- \n" + relievingDateTime);
// 3. difference between 2 LocalDate - Days spent in a organization
Period period = Period.between(
joiningDateTime.toLocalDate(),
relievingDateTime.toLocalDate()
);
System.out.println("\nPeriod between 2 LocalDate is :- \n" + period);
// 3.1 get difference of years/months/days
int years = period.getYears();
int months = period.getMonths();
int days = period.getDays();
// 4. difference between 2 LocalTime - Time duration spent in Office
Duration duration = Duration.between(
joiningDateTime.toLocalTime(),
relievingDateTime.toLocalTime()
);
System.out.println("\nTime Duration between 2 LocalTime is :- \n" + duration);
// 4.1 get difference in nanos/seconds
int nanos = duration.getNano();
long seconds = duration.getSeconds();
// 5. print to console
System.out.print("\nPeriod & Time Duration spent in an Organization is :- \n"
+ years + " Years "
+ months + " Months "
+ days + " Days "
+ seconds + " Seconds "
+ nanos + " Nanos");
}
}
Output:
Default system zone is :-
+05:30
Organization Joining Date/time is :-
2017-03-06T10:32:56.123456789+05:30
Organization Relieving Date/time is :-
2019-06-28T20:31:43.987654321+05:30
Period between 2 LocalDate is :-
P2Y3M22D
Time Duration between 2 LocalTime is :-
PT9H58M47.864197532S
Period & Time Duration spent in an Organization is :-
2 Years 3 Months 22 Days 35927 Seconds 864197532 Nanos
2. Find difference between two Calendar/Date instances :
- First, get organization joining Date/time by instantiating GregorianCalendar and passing Year, Month, Day, Hour, Minute and Second fields/values to the constructor
- Similarly, get 2nd Date/time as organization relieving Date by instantiating GregorianCalendar and passing Year, Month, Day, Hour, Minute and Second fields/values to the constructor
- getTime() method of Date returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this
Date
object - Subtract joining date/time from relieving date/time which returns difference between 2 Dates in terms of milliseconds
- Using this difference in Milliseconds to calculate years, days, hours, minutes and seconds as shown in the below illustration
- Finally print Years/Days/Hours/Minutes/Seconds spent in an organization to the console
FindDifferenceOfTwoCalendarDateTime.java
package in.bench.resources.java8.offsetdatetime.examples;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class FindDifferenceOfTwoCalendarDateTime {
public static void main(String[] args) {
// 1. organization joining date - Instantiate GregorianCalendar
Calendar calendar1 = new GregorianCalendar(2017, 2, 6, 10, 32, 56); // March
Date date1 = calendar1.getTime();
System.out.println("Organization Joining Date/time is :- \n" + date1);
// 2. organization relieving date - Instantiate GregorianCalendar
Calendar calendar2 = new GregorianCalendar(2019, 5, 28, 20, 31, 43); // June
Date date2 = calendar2.getTime();
System.out.println("\nOrganization Relieving Date/time is :- \n" + date2);
// 3. difference between 2 Date/time - Days spent in a organization
long differenceInMillis = date2.getTime() - date1.getTime();
// 3.1 get difference of years/days/hours/minutes/seconds
long years = (differenceInMillis / (365 * 24 * 60 * 60 * 1000L));
long days = (differenceInMillis / (24 * 60 * 60 * 1000L)) % 365;
long hours = (differenceInMillis / (60 * 60 * 1000L)) % 24;
long minutes = (differenceInMillis / (60 * 1000L)) % 60;
long seconds = (differenceInMillis / 1000) % 60;
// 3.2 print to console
System.out.print("\nTime Duration spent in an Office is :- \n"
+ years + " Years "
+ days + " Days "
+ hours + " Hours "
+ minutes + " Minutes "
+ seconds + " Seconds"
);
}
}
Output:
Organization Joining Date/time is :-
Mon Mar 06 10:32:56 IST 2017
Organization Relieving Date/time is :-
Fri Jun 28 20:31:43 IST 2019
Time Duration spent in an Office is :-
2 Years 114 Days 9 Hours 58 Minutes 47 Seconds
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/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Period.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Duration.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
- https://docs.oracle.com/javase/8/docs/api/java/util/GregorianCalendar.html
Happy Coding !!
Happy Learning !!