Java Programs›Date & Time›Calculate Time Difference Between Two Times

Calculate Time Difference Between Two Times in Java

beginner·  Date & Time  ·  Date & Time

Problem

A Duration measures an amount of time in seconds and nanoseconds, independent of any calendar.

Given a check-in at 09:15 and a check-out at 17:40:30, calculate how long the person worked.

Input
09:15 to 17:40:30
Output
8 hours, 25 minutes, 30 seconds

Java Program

Java
import java.time.Duration; import java.time.LocalTime; public class TimeDifferenceDuration { public static void main(String[] args) { LocalTime checkIn = LocalTime.of(9, 15); LocalTime checkOut = LocalTime.of(17, 40, 30); Duration worked = Duration.between(checkIn, checkOut); System.out.println("Duration: " + worked); // ISO form, e.g. PT8H25M30S System.out.println("Hours: " + worked.toHours()); System.out.println("Minutes part: " + worked.toMinutesPart()); // Java 9+ System.out.println("Seconds part: " + worked.toSecondsPart()); System.out.println("Total minutes: " + worked.toMinutes()); } }

Output

Duration: PT8H25M30S Hours: 8 Minutes part: 25 Seconds part: 30 Total minutes: 505

Core Logic

Duration.between gives the exact gap, and its methods break it into whole units or report the total in one unit.

How It Works
  1. 1Both times are LocalTime values.
  2. 2Duration.between(checkIn, checkOut) computes the difference.
  3. 3toHours() gives whole hours; toMinutesPart() and toSecondsPart() (Java 9) give the remainders.
  4. 4toMinutes() gives the whole duration in minutes.
The ISO form PT8H25M30S means 8 hours, 25 minutes and 30 seconds.
💡

Key Point: toMinutes() is the total (505), while toMinutesPart() is only the minutes after the whole hours (25).

Key Concepts

Duration.between()toHours()toMinutesPart()ISO-8601 duration

Approach 2: Shifts That Cross Midnight

Java
import java.time.Duration; import java.time.LocalDateTime; import java.time.LocalTime; public class TimeDifferenceOvernight { public static void main(String[] args) { LocalTime start = LocalTime.of(22, 0); LocalTime end = LocalTime.of(6, 30); System.out.println("Naive LocalTime difference: " + Duration.between(start, end)); // negative! LocalDateTime s = LocalDateTime.of(2026, 3, 9, 22, 0); LocalDateTime e = LocalDateTime.of(2026, 3, 10, 6, 30); // the next morning Duration shift = Duration.between(s, e); System.out.printf("Night shift: %d h %d min%n", shift.toHours(), shift.toMinutesPart()); } }

Output

Naive LocalTime difference: PT-15H-30M Night shift: 8 h 30 min

Core Logic

A time of day alone can't tell that 06:30 is the next morning, so overnight gaps need full date-times.

How It Works
  1. 1Duration.between(22:00, 06:30) with LocalTime returns a negative duration.
  2. 2Using LocalDateTime values on consecutive days gives the real 8½ hours.
  3. 3The result is printed as hours and minutes.
A 22:00 to 06:30 night shift is 8 h 30 min, not −15½ hours.
💡

Key Point: If only times are available, add 24 hours when the end is earlier than the start.

Key Concepts

negative DurationLocalDateTimeovernight intervals

Interview Questions

What's the difference between Duration and Period?

Duration measures exact time in seconds and nanoseconds; Period measures calendar amounts in years, months and days.

How do you format a Duration as hh:mm:ss?

String.format("%02d:%02d:%02d", d.toHours(), d.toMinutesPart(), d.toSecondsPart()).

Learn the Concept

Related Programs