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:
- It returns a positive value when this LocalTime object is greater than the specified LocalTime object.
- It returns a negative value when this LocalTime object is smaller than the specified LocalTime object.
- 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:
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: