Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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:

@Test
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:

@Test
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:

@Test
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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW