I have timespans:
String time1 = 01:00:00
String time2 = 05:00:00
I want to check if time1 and time2 both lies between 20:11:13 and 14:49:00
.
Actually, 01:00:00
is greater than 20:11:13
and less than 14:49:00
considering 20:11:13
is always less than 14:49:00
. This is given prerequisite.
So what I want is, 20:11:13 < 01:00:00 < 14:49:00
.
So I need something like that:
public void getTimeSpans() { boolean firstTime = false, secondTime = false; if(time1 > "20:11:13" && time1 < "14:49:00") { firstTime = true; } if(time2 > "20:11:13" && time2 < "14:49:00") { secondTime = true; } }
I know that this code does not give correct result as I am comparing the string objects.
How to do that as they are the timespans but not the strings to compare?
To check if a date is between two dates: Use the Date() constructor to convert the dates to Date objects. Check if the date is greater than the start date and less than the end date. If both conditions are met, the date is between the two dates.
Or using: checkTime("20:11:13", "14:49:00", "05:00:00"); The result will be: Is in between!
You can use the Calendar
class in order to check.
For example:
try { String string1 = "20:11:13"; Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1); Calendar calendar1 = Calendar.getInstance(); calendar1.setTime(time1); calendar1.add(Calendar.DATE, 1); String string2 = "14:49:00"; Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2); Calendar calendar2 = Calendar.getInstance(); calendar2.setTime(time2); calendar2.add(Calendar.DATE, 1); String someRandomTime = "01:00:00"; Date d = new SimpleDateFormat("HH:mm:ss").parse(someRandomTime); Calendar calendar3 = Calendar.getInstance(); calendar3.setTime(d); calendar3.add(Calendar.DATE, 1); Date x = calendar3.getTime(); if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) { //checkes whether the current time is between 14:49:00 and 20:11:13. System.out.println(true); } } catch (ParseException e) { e.printStackTrace(); }
20:11:13 < 01:00:00 < 14:49:00
LocalTime target = LocalTime.parse( "01:00:00" ) ; Boolean targetInZone = ( target.isAfter( LocalTime.parse( "20:11:13" ) ) && target.isBefore( LocalTime.parse( "14:49:00" ) ) ) ;
java.time.LocalTime
The java.time classes include LocalTime
to represent a time-of-day only without a date and without a time zone.
So what I want is, 20:11:13 < 01:00:00 < 14:49:00.
First we define the boundaries. Your input strings happen to comply with standard ISO 8601 formats. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.
LocalTime start = LocalTime.parse( "20:11:13" ); LocalTime stop = LocalTime.parse( "14:49:00" );
And define our test case, the target 01:00:00
.
LocalTime target = LocalTime.parse( "01:00:00" );
Now we are set up to compare these LocalTime
objects. We want to see if the target is after the later time but before the earlier time. That means middle of the night in this case, between approximately 8 PM and 3 AM the next morning.
Boolean isTargetAfterStartAndBeforeStop = ( target.isAfter( start ) && target.isBefore( stop ) ) ;
That test can be more simply stated as “not between 3 AM and 8 PM”. We could then generalize to any pair of LocalTime
objects where we test for between if the start comes before the stop with a 24-hour clock, and not between if start comes after the stop (as in the case of this Question).
Further more, spans of time are usually handled with the Half-Open approach where the beginning is inclusive while the ending is exclusive. So a "between" comparison, strictly speaking, would be “is the target equal to or later than start AND the target is before stop”, or more simply, “is target not before start AND before stop”.
Boolean isBetweenStartAndStopStrictlySpeaking = ( ( ! target.isBefore( start ) && target.isBefore( stop ) ) ;
If the start is after the stop, within a 24-hour clock, then assume we want the logic suggested in the Question (is after 8 PM but before 3 AM).
if( start.isAfter( stop ) ) { return ! isBetweenStartAndStopStrictlySpeaking ; } else { return isBetweenStartAndStopStrictlySpeaking ; }
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
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes. Hibernate 5 & JPA 2.2 support java.time.
Where to obtain the java.time classes?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With