Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查事件发生的时间容器_Java_Time_Calendar_Jodatime - Fatal编程技术网

Java 检查事件发生的时间容器

Java 检查事件发生的时间容器,java,time,calendar,jodatime,Java,Time,Calendar,Jodatime,我想实现一个小时间容器,它可以容纳两次,一次开始,一次结束, ,我想使用它在多天内对事件进行迭代,以检查事件是否在该特定时间段内发生,我只需在一分钟/范围内工作,因此对象看起来像这样` 时间容器(小时-分钟开始,小时-分钟结束) 时间范围通常为几分钟,如下午13:00至13:10 我遇到的问题是毫秒,因为它们代表一个静态的最后时刻,它们不能用于多天的迭代,我将其用作伪代码: select events that has happened in the timeframe specified in

我想实现一个小时间容器,它可以容纳两次,一次开始,一次结束, ,我想使用它在多天内对事件进行迭代,以检查事件是否在该特定时间段内发生,我只需在一分钟/范围内工作,因此对象看起来像这样` 时间容器(小时-分钟开始,小时-分钟结束)

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

select events that has happened in the timeframe specified in the constructor  regardless of the day and month,
这些事件包含一个日历实例,我想根据小时和分钟进行匹配,有什么建议吗? 我试图用joda time记录thsi,但到目前为止还没有找到方法


谢谢你

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

然后,在代码中,您只需为给定完整的
日期时间的每个事件提取
LocalTime
,并检查它是否在
startTime
之后和
endTime
之前,您可以调用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;

            }

}

如果你想用毫秒来计算,就用mod(%)来表示一天中的毫秒数。如果你想和乔达一起去,你可能会得到一些更可读的东西。见下面的例子

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

    }

 }
公共类测试{
静态类时间容器{
专用静态最终长秒=1000;
专用静态最后长分钟=60*s;
私人静态最终长时间=60*min;
私人静态最终长日=24*小时;
私人最终长启动时间;
私人最终长时间;
公共时间容器(长startHour、长startMinutes、长endHour、长endMinutes){
开始时间=开始小时*小时+开始分钟*分钟;
结束时间=结束小时*小时+结束分钟*分钟+分钟;
}
公共布尔测试(长时间测试){
long hoursInDay=时间测试%day;

return hoursInDay>=starttime&&hoursInDay thx,但是如果我迭代多天,我应该递归地创建新日历,将它们的时间字段设置为指定的小时,并将它们传递给该方法,如果我迭代200天,它将变得非常沉重
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));
        }

    }

 }