Check If a Given Time Lies Between Two Times Regardless of Date

Check if a given time lies between two times regardless of date

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();
}

Java: How to check whether given time lies between two times?

You've used LocalTime which doesn't store date information, only time.
Then you are trying to check if target time is after start time (03:00 after 21:30). This statement is false.

Your start time should be before end time.

If you need to handle night shift try following:

    if (startTime.isAfter(endTime)) {
if (targetTime.isBefore(endTime) || targetTime.isAfter(startTime)) {
System.out.println("Yes! night shift.");
} else {
System.out.println("Not! night shift.");
}
} else {
if (targetTime.isBefore(endTime) && targetTime.isAfter(startTime)) {
System.out.println("Yes! without night shift.");
} else {
System.out.println("Not! without night shift.");
}
}

how to find my current time lies between today's time and tommorow's time in JAVA

java.time.LocalTime is your friend here. Below is a quick example, sure it can be done somewhat shorter.

void test(){
var tz = ZoneId.of("CET");
var anyDate = LocalDate.of(2019,12,4);

var x = ZonedDateTime.of(LocalDateTime.of(anyDate, LocalTime.of(18,59)),tz).toInstant();

System.out.println(testTime(Clock.fixed( ZonedDateTime.of(LocalDateTime.of(anyDate, LocalTime.of(18,59)),tz).toInstant(),tz)));
System.out.println(testTime(Clock.fixed( ZonedDateTime.of(LocalDateTime.of(anyDate, LocalTime.of(19,01)),tz).toInstant(),tz)));
System.out.println(testTime(Clock.fixed( ZonedDateTime.of(LocalDateTime.of(anyDate, LocalTime.of(00,00)),tz).toInstant(),tz)));
System.out.println(testTime(Clock.fixed( ZonedDateTime.of(LocalDateTime.of(anyDate, LocalTime.of(02,59)),tz).toInstant(),tz)));
System.out.println(testTime(Clock.fixed( ZonedDateTime.of(LocalDateTime.of(anyDate, LocalTime.of(03,01)),tz).toInstant(),tz)));
}

boolean testTime(Clock clock){
var evening =LocalTime.of(19,00);
var midnight =LocalTime.of(00,00);
var night =LocalTime.of(03,00);

LocalTime wallTime = LocalTime.now(clock);

return (wallTime.isAfter(evening) && wallTime.isBefore(midnight.minusNanos(1))) || (midnight.isBefore(wallTime) && wallTime.isBefore(night)) || wallTime.equals(midnight);

}

PHP Check if time is between two times regardless of date

Try this function:

function isBetween($from, $till, $input) {
$f = DateTime::createFromFormat('!H:i', $from);
$t = DateTime::createFromFormat('!H:i', $till);
$i = DateTime::createFromFormat('!H:i', $input);
if ($f > $t) $t->modify('+1 day');
return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
}

demo

How to check if time in day is between two times?

You can use Calendar.get(), as mentioned in another answer. To compare minutes, though, you should use Calendar.MINUTE, too:

int minutes_in_day = time.get(Calendar.HOUR_OF_DAY)*60 + time.get(Calendar.MINUTE);

Then, you can just compare the minutes within the day of the current time with that of the start and end times. This will, of course, only work when the times are in the same day.

Need to test if a time is between two other times on same day

Check current time lies in two times in java

The problem is that when you do this

Date date_from = formatter.parse(from);
Date date_to = formatter.parse(to);

and the formatter is so defined:

SimpleDateFormat formatter = new SimpleDateFormat("H:mm:ss");

then is for the java api relevant only the time, but date objects hold more than time info, they hold too year, month, etc and those are getting initalize to a epoch UNIXTime

so the initial dates you are creating are

Thu Jan 01 05:10:00 CET 1970 and Thu Jan 01 10:10:00 CET 1970

so asking if today/right now (22th june 2016) is between those dates will NEVER return true...

on the other hand you conditions look inverted and as final tip you dont need SQL-Classes for the calculation, just with date objects you will get it pretty good

Example:
   public static void main(String[] args) throws ParseException {
String from = "5:10:00";
String to = "10:10:00";
String n = "08:10:00";
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
Date date_from = formatter.parse(from);
Date date_to = formatter.parse(to);
Date dateNow = formatter.parse(n);
if (date_from.before(dateNow) && date_to.after(dateNow)) {
System.out.println("Yes time between");
}
}

Android - Check if current time is between 2 given times and the day of the week is valid

One possible approach could be to normalize all your times to offsets from a chosen week starting point, say Sunday 12:00am = 0. Convert all your times into minutes since the starting point. Then simply check whether your chosen time (also converted to minutes since start) falls within any of the ranges.



Related Topics



Leave a reply



Submit