我想实现一个容纳2次,开始和结束的小时间容器, ,我想使用它在多天的事件上迭代它,以检查是否在该特定时间范围内发生了事件,我将只需要在一分钟/范围内工作,所以该对象看起来像是这样的事情。 timeContainer(小时 - 分钟开始,小时 - 分钟结束);
时间范围通常为几分钟,如下午13:00至13:10 我遇到的问题是毫秒,因为它们代表一个静态的最后时刻,它们不能用于多天的迭代,我会用它作为伪代码:
select events that has happened in the timeframe specified in the constructor regardless of the day and month,
事件包含一个Calendar实例,我想针对小时和分钟执行匹配,任何建议? 我正试图用joda-time来解决这个问题,但到目前为止还没有找到方法
谢谢
答案 0 :(得分:2)
如果你想用毫秒来做,只需使用mod(%)和一天中的毫秒数。如果你想和Joda一起去,你可能会得到一些更具可读性的东西。见下面的例子。
public class Test {
static class TimeContainer {
private static final long second = 1000;
private static final long minute = 60 * second;
private static final long hour = 60 * minute;
private static final long day = 24 * hour;
private final long starttime;
private final long endtime;
public TimeContainer(long startHour, long startMinutes, long endHour, long endMinutes) {
starttime = startHour * hour + startMinutes * minute;
endtime = endHour * hour + endMinutes * minute + minute;
}
public boolean test(long timeToTest) {
long hoursInDay = timeToTest % day;
return hoursInDay >= starttime && hoursInDay <= endtime;
}
}
static class JodaContainer {
private final LocalTime starttime;
private final LocalTime endtime;
public JodaContainer (LocalTime start, LocalTime end) {
starttime = start;
endtime = end;
}
public boolean test(long timeToTest) {
LocalTime lt = new LocalTime(timeToTest);
return lt.equals(starttime) || lt.equals(endtime) || (lt.isAfter(starttime) && lt.isBefore(endtime));
}
}
public static void main(String[] args) {
long[] testTimes1 = new long[5];
long[] testTimes2 = new long[5];
Calendar test1 = Calendar.getInstance(TimeZone.getTimeZone("Etc/Zulu"));
Calendar test2 = Calendar.getInstance();
TimeContainer timeContainer = new TimeContainer(13, 0, 13, 10);
JodaContainer jodaContainer = new JodaContainer(new LocalTime(13,0), new LocalTime(13,10));
test1.set(2010, 10, 5, 13, 6, 20);
test2.set(2010, 10, 5, 13, 6, 20);
testTimes1[0] = test1.getTimeInMillis();
testTimes2[0] = test2.getTimeInMillis();
test1.set(2012, 9, 6, 13, 1, 24);
testTimes1[1] = test1.getTimeInMillis();
test2.set(2012, 9, 6, 13, 1, 24);
testTimes2[1] = test2.getTimeInMillis();
test1.set(2010, 11, 22, 13, 9, 1);
testTimes1[2] = test1.getTimeInMillis();
test2.set(2010, 11, 22, 13, 9, 1);
testTimes2[2] = test2.getTimeInMillis();
test1.set(2012, 10, 5, 13, 26, 20);
testTimes1[3] = test1.getTimeInMillis();
test2.set(2012, 10, 5, 13, 26, 20);
testTimes2[3] = test2.getTimeInMillis();
test1.set(2010, 10, 5, 14, 6, 20);
testTimes1[4] = test1.getTimeInMillis();
test2.set(2010, 10, 5, 14, 6, 20);
testTimes2[4] = test2.getTimeInMillis();
for (long t : testTimes1) {
System.out.println(t + "=" + timeContainer.test(t));
}
System.out.println();
for (long t : testTimes2) {
System.out.println(t + "=" + jodaContainer.test(t));
}
}
}
答案 1 :(得分:0)
听起来你真的想要使用Joda Time的LocalTime
课程:
timeContainer(LocalTime startTime, LocalTime endTime)
然后在您的代码中,您只需要为每个事件LocalTime
提取DateTime
,并检查它是否等于startTime
之前和endTime
之前
答案 2 :(得分:0)
您可以调用Date.before()或Date.after()来检查时间是否在范围内。
boolean isTimeInRange(Calendar startTime, Calendar endTime, Date date ){
if (date.before(endTime.getTime())
&& date.after(startTime.getTime())) {
// time in range
return true;
} else {
return false;
}
}