In this article, we will learn how to find difference between two LocalTime instances up-to Nanosecond precision using newly introduced methods in Java 1.9 version
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,
- toHoursPart() – Extracts the number of hours part in the duration
- toMinutesPart() – Extracts the number of minutes part in the duration
- toSecondsPart() – Extracts the number of seconds part in the duration
- toMillisPart() – Extracts the number of milliseconds part of the duration
- toNanosPart() – get the nanoseconds part within seconds of the duration
- Finally print Time duration spent in a day at office to the console
FindDifferenceOfTwoLocalTime.java
package in.bench.resources.java9.conversion;
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 hours/minutes/seconds/millis/nanos
int hours = duration.toHoursPart();
int minutes = duration.toMinutesPart();
long seconds = duration.toSecondsPart();
int millis = duration.toMillisPart();
int nanos = duration.toNanosPart();
// 3.2 Time spent in hours/minutes/seconds/millis/nanos
// 5. print to console
System.out.print("\nTime Duration spent in an Organization is :- \n"
+ hours + " Hours "
+ minutes + " Minutes "
+ seconds + " Seconds "
+ millis + " Millisecond "
+ nanos + " Nanosecond");
}
}
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 an Organization is :-
9 Hours 2 Minutes 44 Seconds 0 Millisecond 276 Nanosecond
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/9/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Period.html
- https://docs.oracle.com/javase/9/docs/api/java/time/Duration.html
Happy Coding !!
Happy Learning !!