JodaTime - 检查本地时间是现在后,现在又本地时间之前, [英] JodaTime - Check if LocalTime is after now and now before another LocalTime
问题描述
我想检查CURRENTTIME是开始之后本地时间
,另一端前本地时间
这是开始时间加上11小时。这工作正常,如果开始时间为11:00(结束时间将是22:00)。然而,当我尝试比较16:00开始时间和03:00, currentTime.isBefore(endTime的)的结束时间
永远不会被记录,但它应该是
I am trying to check if the currenttime is after a startLocalTime
and before another endLocalTime
which is the start time plus 11 hours. This works fine if the start time is 11:00 (end time will be 22:00). However when I try to compare a start time of 16:00 and an end time of 03:00, currentTime.isBefore(endTime)
will never be logged, but it should be.
LocalTime currentTime = LocalTime.now(); //21:14
LocalTime startTime = new LocalTime( 16, 00, 0, 0);
LocalTime endTime = startTime.plusHours(11); //03:00 Midnight
if(currentTime.isAfter(startTime)){
Log.v("Test", "After startTime");
}
if(currentTime.isBefore(endTime)){
Log.v("Test", "Before endTime");
}
任何想法如何解决这个问题?
Any ideas how to solve this problem?
推荐答案
基本上你需要检查是否你的的startTime
和 endTime的
反转(即,如果 endTime的
是前的startTime
)。如果是,那一定是一个夜间间隔,你应该只把它当作好像它们是倒过来的,并且反转的结果。
Basically you need to check whether your startTime
and endTime
are inverted (i.e. if endTime
is before startTime
). If it is, that must be a "night-time" interval, and you should just treat it as if they were the other way round, and invert the result.
public boolean isWithinInterval(LocalTime start, LocalTime end, LocalTime time) {
if (start.isAfter(end)) {
return !isWithinInterval(end, start, time);
}
// This assumes you want an inclusive start and an exclusive end point.
return start.compareTo(time) <= 0 &&
time.compareTo(end) < 0;
}
现在这里唯一奇怪的是,如图所示,开始时间通常是包容性和结束时间是独一无二 - 而如果我们反转的结果(和参数),我们有一个包容的结束时间,以及独特的开始时间。所以,你可能要处理的案件,而不是明确的:
Now the only oddity here is that as shown, the start time is normally inclusive and the end time is exclusive - whereas if we invert the result (and arguments) we have an inclusive end time and an exclusive start time. So you might want to handle the cases explicitly instead:
public boolean isWithinInterval(LocalTime start, LocalTime end, LocalTime time) {
if (start.isAfter(end)) {
// Return true if the time is after (or at) start, *or* it's before end
return time.compareTo(start) >= 0 ||
time.compareTo(end) < 0;
} else {
return start.compareTo(time) <= 0 &&
time.compareTo(end) < 0;
}
}
(你如何选择哪一个目标,你使用的compareTo
的说法是给你的,当然,还有有效地写相同的code的多种方式。 )
(How you choose which target and argument you use for compareTo
is up to you, of course. There are multiple ways of writing effectively the same code.)
短,但完整的例子:
import org.joda.time.LocalTime;
public class Test {
public static void main(String[] args) {
LocalTime morning = new LocalTime(6, 0, 0);
LocalTime evening = new LocalTime(18, 0, 0);
LocalTime noon = new LocalTime(12, 0, 0);
LocalTime midnight = new LocalTime(0, 0, 0);
System.out.println(isWithinInterval(morning, evening, noon)); // true
System.out.println(
isWithinInterval(morning, evening, midnight)); // false
System.out.println(
isWithinInterval(evening, morning, noon)); // false
System.out.println(
isWithinInterval(evening, morning, midnight)); // true
}
public static boolean isWithinInterval(LocalTime start,
LocalTime end,
LocalTime time) {
if (start.isAfter(end)) {
// Return true if the time is after (or at) start,
// *or* it's before end
return time.compareTo(start) >= 0 ||
time.compareTo(end) < 0;
} else {
return start.compareTo(time) <= 0 &&
time.compareTo(end) < 0;
}
}
}
这篇关于JodaTime - 检查本地时间是现在后,现在又本地时间之前,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!