1

我想实现一个包含 2 次的小时间容器,一个开始和一个结束,我想用它在多天的事件中迭代它,以检查在特定时间范围内是否发生了事件,我将不得不仅在一分钟/范围内工作,因此对象看起来像这样

时间范围通常是几分钟,比如下午 13:00 到下午 13:10 我遇到的问题是毫秒,因为它们代表静态的最后时刻,它们不能用于多天的迭代,我会将其用作伪代码:

select events that has happened in the timeframe specified in the constructor  regardless of the day and month,

事件包含一个日历实例,我想对小时和分钟进行匹配,有什么建议吗?我正在尝试用 joda-time 来恢复,但到目前为止还没有找到方法

谢谢

4

3 回答 3

2

如果您想以毫秒为单位,只需使用带有一天中毫秒数的 mod (%)。如果您想使用 Joda-time,您可能会得到更具可读性的内容。请参阅下面的示例。

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

    }

 }
于 2012-10-22T16:17:27.793 回答
0

听起来您真的很想使用 Joda Time 的LocalTime课程:

timeContainer(LocalTime startTime, LocalTime endTime)

然后在您的代码中,您只需要提取LocalTime每个事件的 full DateTime,并检查它是否在 after 或等于startTimeand before endTime

于 2012-10-22T14:42:56.953 回答
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;

            }

}
于 2012-10-22T14:57:28.113 回答