【问题标题】:Time container to check event occurrence检查事件发生的时间容器
【发布时间】:2012-10-22 14:39:59
【问题描述】:

我想实现一个小时间容器,它可以容纳 2 次,一个开始和一个结束, ,我想用它在多天的事件上迭代它,以检查事件是否在那个特定的时间范围内发生,我只需要在一分钟/范围内工作,所以对象看起来就像是这样的` timeContainer(小时-分钟开始,小时-分钟结束);

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

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

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

谢谢

【问题讨论】:

    标签: java time calendar jodatime


    【解决方案1】:

    如果您想以毫秒为单位,只需使用带一天毫秒数的 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));
            }
    
        }
    
     }
    

    【讨论】:

      【解决方案2】:

      听起来你真的很想使用 Joda Time 的 LocalTime 类:

      timeContainer(LocalTime startTime, LocalTime endTime)
      

      然后在您的代码中,您只需要提取每个事件的LocalTime 并给出完整的DateTime,并检查它是否在startTime 之后和endTime 之前。

      【讨论】:

        【解决方案3】:

        您可以调用 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;
        
                    }
        
        }
        

        【讨论】:

        • thx,但是如果我迭代多天,我应该递归地创建新日历并将它们的时间字段设置为指定的小时并将它们传递给方法,如果我迭代超过 200 天,它会变得非常非常沉重
        猜你喜欢
        • 2010-09-20
        • 2017-02-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多