In this article, we will learn how to find difference between two LocalTime and Calendar/Date instances
1. Find difference between two LocalTime instances :
- To find difference between two LocalTime instances, we can use Duration.between() method
- Duration.between accepts 2 input-arguments as LocalTime and returns difference of two LocalTime as Duration
- 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
- Finally print following to the console
- Time Duration spent as number of seconds & nanoseconds in an office
- Number of hours within Duration leaving Minutes/Millis/Nanos
- Number of minutes within Duration leaving Millis/Nanos
- Number of millis within Duration leaving Nanos
- Number of nanos within Duration
FindDifferenceOfTwoLocalTime.java
package in.bench.resources.java8.localtime.examples;
import java.time.Duration;
import java.time.LocalTime;
public class FindDifferenceOfTwoLocalTime {
public static void main(String[] args) {
// 1. office start time
LocalTime startsTime = LocalTime.of(8, 29, 15, 723);
System.out.println("Office start Time is :- \n" + startsTime);
// 2. office end time
LocalTime endsTime = LocalTime.of(17, 31, 59, 999);
System.out.println("\nOffice end Time is :- \n" + endsTime);
// 3. difference between 2 LocalTime - Time duration spent in Office
Duration duration = Duration.between(startsTime, endsTime);
System.out.println("\nTime Duration between 2 LocalTime is :- \n" + duration);
// 3.1 get difference in nanos/seconds
int nanos = duration.getNano();
long seconds = duration.getSeconds();
// 3.2 Time spent in Seconds & Nanoseconds
System.out.println("\nTime Duration spent in Office is :- \n"
+ seconds + " Seconds "
+ nanos + " Nanos");
// 3.3 Number of hours within Duration leaving Minutes/Millis/Nanos
System.out.println("\nNumber of hours within Duration leaving Minutes/Millis/Nanos :- \n"
+ duration.toHours());
// 3.4 Number of minutes within Duration leaving Millis/Nanos
System.out.println("\nNumber of minutes within Duration leaving Millis/Nanos :- \n"
+ duration.toMinutes());
// 3.5 Number of Millis within Duration leaving Nanos
System.out.println("\nNumber of millis within Duration leaving Nanos :- \n"
+ duration.toMillis());
// 3.5 Number of Nanos within Duration
System.out.print("\nNumber of nanos within Duration :- \n"
+ duration.toNanos());
}
}
Output:
Office start Time is :-
08:29:15.000000723
Office end Time is :-
17:31:59.000000999
Time Duration between 2 LocalTime is :-
PT9H2M44.000000276S
Time Duration spent in Office is :-
32564 Seconds 276 Nanos
Number of hours within Duration leaving Minutes/Millis/Nanos :-
9
Number of minutes within Duration leaving Millis/Nanos :-
542
Number of millis within Duration leaving Nanos :-
32564000
Number of nanos within Duration :-
32564000000276
2. Find difference between two Calendar/Date instances :
- First, get Office start Time by instantiating GregorianCalendar and passing Year, Month, Day, Hour, Minute and Second fields/values to the constructor
- Similarly, get 2nd Date as Office end Time 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
- getTime() method of Date returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this
- Subtract start date/time from end date/time which returns difference between 2 Dates in terms of milliseconds
- Using this difference in Milliseconds, calculate seconds, minutes and hours as shown in the below illustration
- Finally print following to the console
- Difference in Milliseconds
- Difference in Seconds
- Difference in Minutes
- Difference in Hours
FindDifferenceOfTwoCalendarDate.java
package in.bench.resources.java8.localtime.examples;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class FindDifferenceOfTwoCalendarDateTime {
public static void main(String[] args) {
// 1. office start time - Instantiate GregorianCalendar
Calendar calendar1 = new GregorianCalendar(2022, 7, 7, 9, 29, 15); // 7-August
Date date1 = calendar1.getTime();
System.out.println("Office start Time is :- \n" + date1);
// 2. office end time - Instantiate GregorianCalendar
Calendar calendar2 = new GregorianCalendar(2022, 7, 7, 17, 31, 29); // 7-August
Date date2 = calendar2.getTime();
System.out.println("\nOffice end Time is :- \n" + date2);
// 3. difference between 2 Date - time duration spent in an Office
long differenceInMillis = date2.getTime() - date1.getTime();
System.out.println("\nDifference in Milliseconds is :- \n" + differenceInMillis);
// 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:
Office start Time is :-
Sun Aug 07 09:29:15 IST 2022
Office end Time is :-
Sun Aug 07 17:31:29 IST 2022
Difference in Milliseconds is :-
28934000
Time Duration spent in an Office is :-
0 Years 0 Days 8 Hours 2 Minutes 14 Seconds
Related Articles:
- Java 8 – LocalTime with method details and examples
- Java 8 – How to get Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to form LocalTime passing Hour, Minute and Second fields ?
- Java 8 – How to parse LocalTime in String form ?
- Java 8 – How to convert String to LocalTime ?
- Java 8 – How to convert LocalTime to String ?
- Java 8 – How to convert LocalTime in different formats ?
- Java 8 – How to convert LocalTime in different Format Style ?
- Java 8 – How to convert LocalTime to LocalDateTime ?
- Java 8 – How to convert LocalTime to ZonedDateTime ?
- Java 8 – How to convert LocalTime to an OffsetDateTime ?
- Java 8 – How to convert LocalTime to an Instant ?
- Java 8 – How to convert LocalTime to an OffsetTime ?
- Java 8 – How to convert LocalTime to Seconds and vice-versa ?
- Java 8 – How to convert LocalTime to Nanoseconds and vice-versa ?
- Java 8 – How to convert LocalTime to java.util.Date and vice-versa ?
- Java 8 – How to convert LocalTime to java.sql.Timestamp and vice-versa ?
- Java 8 – How to convert LocalTime to Calendar and vice-versa ?
- Java 8 – How to convert LocalTime to GregorianCalendar and vice-versa ?
- Java 8 – How to convert LocalTime to XMLGregorianCalendar and vice-versa ?
- Java 8 – How to convert java.util.Date to LocalTime in different ways ?
- Java 8 – How to add Hour, Minute and Second fields to LocalTime ?
- Java 8 – How to subtract Hour, Minute and Second fields from LocalTime ?
- Java 8 – How to alter Hour, Minute and Second fields of LocalTime ?
- Java 8 – How to check whether a LocalTime is Before another LocalTime ?
- Java 8 – How to check whether a LocalTime is After another LocalTime ?
- Java 8 – How to compare two LocalTime instances ?
- Java 8 – How to find time duration between two LocalTime instances ?
- Java 8 – What are all the Temporal Fields supported by LocalTime ?
- Java 8 – What are all the Temporal Units supported by LocalTime ?
- Java 9 – Find difference between two LocalTime instances upto nanosecond precision ?
- More Java 8 Date/Time API examples
References:
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.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 !!