How can I check if specific time will occur between two dates, for example: time -> 11:34 dates 1.12 17:00 <-> 2.12 17:01
-
3It appears you didn't attempt much research on the subject. – achAmháin Dec 14 '17 at 07:46
-
2Please show what you've already tried, if anything, and clarify whether these are local to a particular time zone or not. For example, between 00:30 and 02:30 on the same date in a particular time zone, there could be 1 hour, 2 hours or 3 hours of elapsed time, if the UTC offset changes. – Jon Skeet Dec 14 '17 at 07:47
-
2Possible duplicate of [Time comparison](https://stackoverflow.com/questions/2309558/time-comparison) – Tu.Ma. Dec 14 '17 at 07:47
-
Currently i'm somewhere around, please consider that i'm searching specific TIME between TWO dates – PDS Dec 14 '17 at 08:14
-
It’s not crystal clear. Do you want to determine whether or not the time 11:34 occurs some time between 1 Dec 17:00 and 2 Dec 17:01? – Ole V.V. Dec 14 '17 at 08:37
-
What have you tried? What did your search and research bring up? What have you considered and why did you discard it? So we don’t repeat what you already know, but rather help you on from the point where you are now. Please reread [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – Ole V.V. Dec 14 '17 at 08:39
3 Answers
The idea would be calculating the dates between start and end date. Then pair it with your specific time and check if any date time matches the following constraint: start <= date + time <= end
.
public boolean isTimeInBetween(LocalDateTime start, LocalDateTime end, LocalTime time) {
return start.toLocalDate().datesUntil(end.plusDays(1).toLocalDate())
.anyMatch(d -> !(d.atTime(time).isBefore(start) || d.atTime(time).isAfter(end)));
}

- 11,480
- 3
- 45
- 62
LocalDateTime startDateTime = LocalDateTime.of(2017, Month.DECEMBER, 1, 17, 0);
LocalDateTime endDateTime = LocalDateTime.of(2017, Month.DECEMBER, 2, 17, 1);
LocalTime timeToTest = LocalTime.of(11, 34);
// Does the timeToTest occur some time between startDateTime and endDateTime?
LocalDateTime candidateDateTime = startDateTime.with(timeToTest);
if (candidateDateTime.isBefore(startDateTime)) {
// too early; try next day
candidateDateTime = candidateDateTime.plusDays(1);
}
if (candidateDateTime.isAfter(endDateTime)) {
System.out.println("No, " + timeToTest + " does not occur between " + startDateTime + " and " + endDateTime);
} else {
System.out.println("Yes, the time occurs at " + candidateDateTime);
}
This prints
Yes, the time occurs at 2017-12-02T11:34
It’s a little bit tricky. I am exploiting the fact that LocalTime
implements the TemporalAdjuster
interface, which allows me to adjust one into another date-time class, in this case startDateTime
. I don’t know at first whether this will adjust the time forward or backward, so I need to test that in a subsequent if
statement.
Please consider whether you wanted your date-time interval to be inclusive/closed, exclusive/open or half-open. The standard recommendation is the last: include the start time, exclude the end time; but only you know your own requirements.
Also be aware that using LocalDateTime
prevents taking summer time (DST) and other transitions into account. For example, if moving the clock forward in spring, some times of day will not exist that day, but the above code will be happy to tell you they do exist.

- 81,772
- 15
- 137
- 161
-
-
That was my question too, @LukaRahne. :-) As my code stands, it will say that the time does occur (provided that `endDateTime` isn’t earlier). If you don’t want that, change `candidateDateTime.isBefore(startDateTime)` to `! candidateDateTime.isAfter(startDateTime)` (*not after* meaning *before or on*). – Ole V.V. Dec 15 '17 at 09:08
You can define 3 variables, start, end and a test time. Using Java 8's LocaleDateTime makes this simple enough. See example below with 3 test cases:
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2017, 12, 1, 17, 0);
LocalDateTime end = LocalDateTime.of(2017, 12, 2, 17, 1);
System.out.println("Test with time before range");
System.out.println(isInRange(start, end, LocalDateTime.of(2017, 12, 1, 12, 0)));
System.out.println("Test with time in range");
System.out.println(isInRange(start, end, LocalDateTime.of(2017, 12, 2, 11, 34)));
System.out.println("Test with time after range");
System.out.println(isInRange(start, end, LocalDateTime.of(2017, 12, 2, 20, 0)));
}
private static boolean isInRange(LocalDateTime start, LocalDateTime end, LocalDateTime test) {
return !(test.isBefore(start) || test.isAfter(end));
}
Output:
Test with time before range
false
Test with time in range
true
Test with time after range
false