Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Introduction

In Java, there are numerous ways to determine if a particular time lies within the two times without considering dates.

In this tutorial, we’ll look at several possible ways to achieve this.

2. Using the isAfter() and isBefore() Methods

Java 8 introduced the LocalTime class in the java.time package, providing convenient methods to work with time without considering the date:

LocalTime startTime = LocalTime.parse("09:00:00");
LocalTime endTime = LocalTime.parse("17:00:00");
LocalTime targetTime = LocalTime.parse("12:30:00");

Here, we initialize three LocalTime variables, startTime, endTime, and targetTime, with specific time values. These lines essentially set the starting time, ending time, and target time for comparison in the test methods.

LocalTime.parse(“09:00:00”) parses the string “09:00:00” into a LocalTime object representing 9:00 AM. Similarly, “17:00:00” represents 5:00 PM, and “12:30:00” represents 12:30 PM.

Let’s now test this approach:

void givenLocalTime_whenUsingIsAfterIsBefore_thenTimeIsBetween() {
    assertTrue(!targetTime.isBefore(startTime) && !targetTime.isAfter(endTime));

This method tests whether a given targetTime falls between a specified startTime and endTime using the isAfter() and isBefore() methods of the LocalTime class. We use the assetTrue() method to verify that the targetTime is neither before the startTime nor after the endTime.

3. Using the compareTo() Method

Another approach is to use the compareTo() method, which compares two LocalTime instances:

void givenLocalTime_whenUsingCompareTo_thenTimeIsBetween() {
    assertTrue(targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0);

Here, the expression targetTime.compareTo(startTime) >= 0 && targetTime.compareTo(endTime) <= 0 combines two comparisons using logical AND (&&). It checks whether targetTime is greater than or equal to startTime and less than or equal to endTime.

This compound condition ensures that targetTime falls within the time range defined by startTime and endTime, inclusive. If both individual comparisons evaluate to true, the entire expression evaluates to true, indicating that the target time is between the start and end times, or coincides with them.

4. Using the after() and before() Methods

In this approach, we’ll use the legacy Date and Calendar objects to represent and compare times. Although this approach is considered less convenient than modern Java 8 LocalTime methods, it remains relevant for scenarios requiring compatibility with older codebases or systems:

void givenDate_whenUsingAfterBefore_thenTimeIsBetween() {
    Calendar startCalendar = Calendar.getInstance();
    startCalendar.set(Calendar.HOUR_OF_DAY, 9);
    startCalendar.set(Calendar.MINUTE, 0);
    Date startTime = startCalendar.getTime();

    Calendar endCalendar = Calendar.getInstance();
    endCalendar.set(Calendar.HOUR_OF_DAY, 17);
    endCalendar.set(Calendar.MINUTE, 0);
    Date endTime = endCalendar.getTime();

    Calendar targetCalendar = Calendar.getInstance();
    targetCalendar.set(Calendar.HOUR_OF_DAY, 12);
    targetCalendar.set(Calendar.MINUTE, 30);
    Date targetTime = targetCalendar.getTime();

    assertTrue(!targetTime.before(startTime) && !targetTime.after(endTime));

Firstly, we create Calendar instances for the startTime, endTime, and targetTime using the Calendar.getInstance() method, which returns a Calendar object initialized with the current date and time.

Once the Calendar objects are set up, we extract Date objects representing the start, end, and target times from their respective Calendar instances.

With the Date objects prepared, we compare the targetTime against the startTime and endTime. We achieve this by utilizing the Date class’s before() and after() methods.

5. Conclusion

In conclusion, Java offers multiple approaches, including modern LocalTime methods, legacy Date functionalities, and regular expressions, to accurately determine if a specific time falls within defined boundaries, catering to diverse development requirements.

As usual, the accompanying source code can be found over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.