How to compare current time with another time in Java?

tl;dr

LocalTime
    .now()
    .isAfter( 
        LocalTime.parse( "15:30" ) 
    )

Details

You should be thinking the other way around: How to get that string turned into a time value. You would not attempt math by turning your numbers into strings. So too with date-time values.

Avoid the old bundled classes, java.util.Date and .Calendar as they are notoriously troublesome, flawed both in design and implementation. They are supplanted by the new java.time package in Java 8. And java.time was inspired by Joda-Time.

Both java.time and Joda-Time offer a class to capture a time-of-day without any date to time zone: LocalTime.

java.time

Using the java.time classes built into Java, specifically LocalTime. Get the current time-of-day in your local time zone. Construct a time-of-day per your input string. Compare with the isBefore, isAfter, or isEqual methods.

LocalTime now = LocalTime.now();
LocalTime limit = LocalTime.parse( "15:30" );
Boolean isLate = now.isAfter( limit );

Better to specify your desired/expected time zone rather than rely implicitly on the JVM’s current default time zone.

ZoneId z = ZoneId.of( "Pacific/Auckland" ) ;
LocalTime now = LocalTime.now( z );  // Explicitly specify the desired/expected time zone.
LocalTime limit = LocalTime.parse( "15:30" );
Boolean isLate = now.isAfter( limit );

Joda-Time

The code in this case using the Joda-Time library happens to be nearly the same as the code seen above for java.time.

Beware that the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, Calendar, & SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8, Java SE 9, and later
  • Built-in.
  • Part of the standard Java API with a bundled implementation.
  • Java 9 adds some minor features and fixes.
  • Java SE 6 and Java SE 7
  • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
  • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
  • See How to use ThreeTenABP….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Most of the time we need to compare two date and date-time objects. Date comparison is required when we want to get the data of some specific date and time from the database or to filter the returned data based on date and time.

In order to compare time, we use the compareTo() method of the LocalTime class. The compareTo() method of the class compares two LocalTime objects. The first LocalTime object is one which we want to compare, and the second object is which we pass to the compareTo() method as a parameter and from which we want to compare this LocalTime object.

Syntax:

The compareTo() method of the LocalTime class is as follows:

Parameters:

It accepts only a single parameter, i.e., the LocalTime object, which is going to be compared, and it should not be null.

Returns:

The compareTo() method returns three values based on the comparison of the objects:

  1. It returns a positive value when this LocalTime object is greater than the specified LocalTime object.
  2. It returns a negative value when this LocalTime object is smaller than the specified LocalTime object.
  3. It returns zero when this LocalTime object is equal to the specified LocalTime object.

Let's take some examples to understand how the compareTo() method is used for the comparison of two LocalTime objects.

CompareToExample1.java

Output:

How to compare current time with another time in Java?

Let's take one more example to understand the concept of the compareTo() method. In this example, we will compare two date-time objects by using the compareTo() method.

CompareToExample2.java

Output:

How to compare current time with another time in Java?

How to compare two different time in Java?

In order to compare time, we use the compareTo() method of the LocalTime class. The compareTo() method of the class compares two LocalTime objects.

How to compare Date and time with current Date in Java?

Date , we can use compareTo , before() , after() and equals() to compare two dates..
1.1 Date. compareTo. ... .
1.2 Date. before(), Date. ... .
1.3 Check if a date is within a certain range. The below example uses getTime() to check if a date is within a certain range of two dates (inclusive of startDate and endDate)..

How to compare time in String format in Java?

One of the constructors of this class accepts a String value representing the desired date format and creates SimpleDateFormat object. To parse/convert a string as a Date object Instantiate this class by passing desired format string. Parse the date string using the parse() method.

How to compare current Date and time with another Date and time in Android?

By Calendar Object: getTime(); Date date2 = calendar2. getTime(); Then compare both date using compareTo, before(date) or after(date).