DateTime Joda比较

时间:2017-06-20 07:51:50

标签: java datetime comparison jodatime

我有一个包含两个属性的类对象列表:timeStarttimeEnd

它们都是Joda DateTime个对象。

我试图只获取timeStartbeforeNow&& timeEndafterNowtimeStartafterNow()&& timeEndafterNow()

但严肃地说,我得到了大部分物品,即使没有通过我的条件(理论上)。

我猜问题在于内置JodaTime函数(Timezones?)

的比较

代码:

Main.log.info("Now: " + DateTime.now());
    for(ChannelProgramBase base : programs) {

        if(base.getTimeStart().isBeforeNow() && base.getTimeEnd().isAfterNow() || base.getTimeStart().isAfterNow() && base.getTimeEnd().isAfterNow()) {


            Main.log.info("Add: " + base.getTimeStart() + " - " + base.getTimeEnd());
            programsToPlay.add(base);
        }
    }

相应的输出:

[INFO ] 2017-06-20 09:44:16.941 [PlayerHandler-Thread]  line 51 - Now: 2017-06-20T09:44:16.941+02:00
[INFO ] 2017-06-20 09:44:16.942 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T00:00:00.000Z - 2017-06-20T23:55:00.000Z
[INFO ] 2017-06-20 09:44:16.942 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T07:45:00.000Z - 2017-06-20T07:50:00.000Z
[INFO ] 2017-06-20 09:44:16.942 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T08:00:00.000Z - 2017-06-20T08:05:00.000Z
[INFO ] 2017-06-20 09:44:16.942 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T08:15:00.000Z - 2017-06-20T08:20:00.000Z
[INFO ] 2017-06-20 09:44:16.942 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T08:30:00.000Z - 2017-06-20T08:35:00.000Z
[INFO ] 2017-06-20 09:44:16.942 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T08:45:00.000Z - 2017-06-20T08:50:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T09:00:00.000Z - 2017-06-20T09:05:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T09:15:00.000Z - 2017-06-20T09:20:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T09:35:00.000Z - 2017-06-20T09:40:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T10:00:00.000Z - 2017-06-20T10:05:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T10:15:00.000Z - 2017-06-20T10:20:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T10:30:00.000Z - 2017-06-20T10:35:00.000Z
[INFO ] 2017-06-20 09:44:16.943 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T10:45:00.000Z - 2017-06-20T10:50:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T11:00:00.000Z - 2017-06-20T11:05:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T11:15:00.000Z - 2017-06-20T11:20:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T11:30:00.000Z - 2017-06-20T11:35:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T11:45:00.000Z - 2017-06-20T11:50:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T12:00:00.000Z - 2017-06-20T12:05:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T12:15:00.000Z - 2017-06-20T12:20:00.000Z
[INFO ] 2017-06-20 09:44:16.944 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T12:30:00.000Z - 2017-06-20T12:35:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T12:45:00.000Z - 2017-06-20T12:50:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T13:00:00.000Z - 2017-06-20T13:05:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T13:15:00.000Z - 2017-06-20T13:20:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T13:30:00.000Z - 2017-06-20T13:35:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T13:45:00.000Z - 2017-06-20T13:50:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T14:00:00.000Z - 2017-06-20T14:05:00.000Z
[INFO ] 2017-06-20 09:44:16.945 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T14:15:00.000Z - 2017-06-20T14:20:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T14:30:00.000Z - 2017-06-20T14:35:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T14:45:00.000Z - 2017-06-20T14:50:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T15:00:00.000Z - 2017-06-20T15:05:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T15:15:00.000Z - 2017-06-20T15:20:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T15:30:00.000Z - 2017-06-20T15:35:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T15:45:00.000Z - 2017-06-20T15:50:00.000Z
[INFO ] 2017-06-20 09:44:16.946 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T16:00:00.000Z - 2017-06-20T16:05:00.000Z
[INFO ] 2017-06-20 09:44:16.947 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T16:15:00.000Z - 2017-06-20T16:20:00.000Z
[INFO ] 2017-06-20 09:44:16.947 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T16:30:00.000Z - 2017-06-20T16:35:00.000Z
[INFO ] 2017-06-20 09:44:16.947 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T16:45:00.000Z - 2017-06-20T16:50:00.000Z
[INFO ] 2017-06-20 09:44:16.947 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T17:00:00.000Z - 2017-06-20T17:05:00.000Z
[INFO ] 2017-06-20 09:44:16.947 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T17:15:00.000Z - 2017-06-20T17:20:00.000Z
[INFO ] 2017-06-20 09:44:16.947 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T17:30:00.000Z - 2017-06-20T17:35:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T17:45:00.000Z - 2017-06-20T17:50:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T18:00:00.000Z - 2017-06-20T18:05:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T18:15:00.000Z - 2017-06-20T18:20:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T18:30:00.000Z - 2017-06-20T18:35:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T18:45:00.000Z - 2017-06-20T18:50:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T19:00:00.000Z - 2017-06-20T19:05:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T19:15:00.000Z - 2017-06-20T19:20:00.000Z
[INFO ] 2017-06-20 09:44:16.948 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T19:30:00.000Z - 2017-06-20T19:35:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T19:45:00.000Z - 2017-06-20T19:50:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T20:00:00.000Z - 2017-06-20T20:05:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T20:15:00.000Z - 2017-06-20T20:20:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T20:30:00.000Z - 2017-06-20T20:35:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T20:45:00.000Z - 2017-06-20T20:50:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T21:00:00.000Z - 2017-06-20T21:05:00.000Z
[INFO ] 2017-06-20 09:44:16.949 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T21:15:00.000Z - 2017-06-20T21:20:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T21:30:00.000Z - 2017-06-20T21:35:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T21:45:00.000Z - 2017-06-20T21:50:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T22:00:00.000Z - 2017-06-20T22:05:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T22:15:00.000Z - 2017-06-20T22:20:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T22:30:00.000Z - 2017-06-20T22:35:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T22:45:00.000Z - 2017-06-20T22:50:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T23:00:00.000Z - 2017-06-20T23:05:00.000Z
[INFO ] 2017-06-20 09:44:16.950 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T23:15:00.000Z - 2017-06-20T23:20:00.000Z
[INFO ] 2017-06-20 09:44:16.951 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T23:30:00.000Z - 2017-06-20T23:35:00.000Z
[INFO ] 2017-06-20 09:44:16.951 [PlayerHandler-Thread]  line 57 - Add: 2017-06-20T23:45:00.000Z - 2017-06-20T23:50:00.000Z

1 个答案:

答案 0 :(得分:1)

试试这个:

Main.log.info("Now: " + DateTime.now());
    for(ChannelProgramBase base : programs) {

        if((base.getTimeStart().isBeforeNow() && base.getTimeEnd().isAfterNow()) || (base.getTimeStart().isAfterNow() && base.getTimeEnd().isAfterNow())) {


            Main.log.info("Add: " + base.getTimeStart() + " - " + base.getTimeEnd());
            programsToPlay.add(base);
        }
    }