Write a Java Program to Calculate Difference Between Two Time Periods

In Java, you can easily calculate the difference between two time periods using the built-in class, Duration.

Duration represents a time-based amount of time, such as “2 hours and 30 minutes”.

To calculate the difference between two time periods, you need to first create two LocalTime objects representing the starting and ending times.

LocalTime is a class that represents a time without a date, such as “10:30 AM”.

Once you have the LocalTime objects, you can use the Duration.between() method to calculate the duration between them.

This method returns a Duration object, which represents the time difference between two times.

Here is an example Java program that calculates the difference between two time periods:

import java.time.Duration;
import java.time.LocalTime;

public class TimeDifference {
    public static void main(String[] args) {
        LocalTime startTime = LocalTime.of(10, 30); // 10:30 AM
        LocalTime endTime = LocalTime.of(14, 15); // 2:15 PM

        Duration duration = Duration.between(startTime, endTime);
        long minutes = duration.toMinutes(); // get duration in minutes

        System.out.println("The time difference between " + startTime + " and " + endTime + " is " + minutes + " minutes.");
    }
}

In this program, we create two LocalTime objects representing the starting and ending times: startTime and endTime.

We then use the Duration.between() method to calculate the duration between them and store it in the duration variable.

Finally, we convert the duration to minutes using the toMinutes() method and print the result.

You can customize this program to calculate the difference between any two time periods by changing the values of the startTime and endTime variables.

Here’s another example that uses user input to get the starting and ending times:

import java.time.Duration;
import java.time.LocalTime;
import java.util.Scanner;

public class TimeDifference {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the starting time (HH:MM AM/PM): ");
        String startInput = scanner.nextLine();
        LocalTime startTime = LocalTime.parse(startInput, DateTimeFormatter.ofPattern("h:mm a"));

        System.out.println("Enter the ending time (HH:MM AM/PM): ");
        String endInput = scanner.nextLine();
        LocalTime endTime = LocalTime.parse(endInput, DateTimeFormatter.ofPattern("h:mm a"));

        Duration duration = Duration.between(startTime, endTime);
        long minutes = duration.toMinutes();

        System.out.println("The time difference between " + startTime + " and " + endTime + " is " + minutes + " minutes.");

        scanner.close();
    }
}

In this example, we use a Scanner object to get user input for the starting and ending times.

We then parse the user input using the DateTimeFormatter class to create LocalTime objects for the startTime and endTime.

We then use the Duration.between() method to calculate the duration between them and print the result.

By using the Duration class and LocalTime class in Java, it becomes easy to calculate the difference between two time periods.

With these classes, you can handle time calculations with precision and ease, making your programs more efficient and reliable.

It’s worth noting that the Duration class in Java can handle time differences that span multiple days, not just hours and minutes.

For example, if you have a starting time of “9:00 PM” on one day and an ending time of “3:00 AM” on the next day, the Duration.between() method will still return the correct time difference.

Here’s an example:

import java.time.Duration;
import java.time.LocalDateTime;

public class TimeDifference {
    public static void main(String[] args) {
        LocalDateTime startTime = LocalDateTime.of(2023, 2, 17, 21, 0); // 9:00 PM on Feb. 17th
        LocalDateTime endTime = LocalDateTime.of(2023, 2, 18, 3, 0); // 3:00 AM on Feb. 18th

        Duration duration = Duration.between(startTime, endTime);
        long hours = duration.toHours();

        System.out.println("The time difference between " + startTime + " and " + endTime + " is " + hours + " hours.");
    }
}

In this example, we use the LocalDateTime class to represent the starting and ending times, which include both date and time information.

We then use the Duration.between() method to calculate the duration between them and print the result in hours.


Conclusion

In conclusion, the Duration and LocalTime classes in Java provide a powerful and easy-to-use way to calculate time differences.

Whether you’re working with hours and minutes or longer time spans that cross multiple days, these classes can handle it all.

By incorporating these classes into your Java programs, you can make your code more efficient, reliable, and accurate when dealing with time-based calculations.