我使用一个json文件来存储会议的多个条目。我正在尝试在Java中的日历对象中获取使用日期之前和日期的当前事件。我当前的代码返回三个不同的事件,我不知道,我想知道有没有人有任何建议,我已经尝试了一切。我将在下面附加json和代码。我还将把代码放入控制台窗口。
控制台日志

11-22 15:54:12.883 5797-5797/reloaded D/timetest:
    add event with name:Coffee Break
    time start: Tue Nov 22 15:30:00 GMT+00:00 18
    time end: Wed Nov 23 16:30:00 GMT+00:00 18
    time now: Wed Nov 23 15:45:00 GMT+00:00 18
11-22 15:54:12.885 5797-5797/reloaded D/timetest:
    add event with name:Panel Discussion
    time start: Tue Nov 22 16:30:00 GMT+00:00 18
    time end: Wed Nov 23 17:15:00 GMT+00:00 18
    time now: Wed Nov 23 15:45:00 GMT+00:00 18
11-22 15:54:12.888 5797-5797/reloaded D/timetest:
    add event with name:Turning Information into an Engaging Experience – The Design of Media Spaces
    time start: Tue Nov 22 17:15:00 GMT+00:00 18
    time end: Wed Nov 23 18:00:00 GMT+00:00 18
    time now: Wed Nov 23 15:45:00 GMT+00:00 18

杰森
{
      "eventid": "11",
      "roomid": "9",
      "type": "other",
      "name": "Coffee Break",
      "timestart": "15:30",
      "timeend": "16:30",
      "day": "1",
      "text": "Discover the Future Forum",
      "pic": "coffeecup.png",
      "speakerid": "1",
      "speaker": ""
    },
    {
      "eventid": "12",
      "roomid": "9",
      "type": "other",
      "name": "Panel Discussion",
      "timestart": "16:30",
      "timeend": "17:15",
      "day": "1",
      "text": "",
      "pic": "welcometalk.png",
      "speakerid": "1",
      "speaker": "fudged"
    },
    {
      "eventid": "13",
      "roomid": "9",
      "type": "lecture",
      "name": "Turning Information into an Engaging Experience – The Design of Media Spaces",
      "timestart": "17:15",
      "timeend": "18:00",
      "day": "1",
      "text": "fdsfsd",
      "pic": "p4.jpg",
      "speakerid": "6",
      "speaker": "test"
    },

爪哇
try {
    String string1 = "22/11/18 " + c.getString("timestart");
    Date time1 = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(string1);
    Calendar calendar1 = Calendar.getInstance();
    calendar1.setTime(time1);

    String string2 = "22/11/18 " + c.getString("timeend");
    Date time2 = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(string2);
    Calendar calendar2 = Calendar.getInstance();
    calendar2.setTime(time2);
    calendar2.add(Calendar.DATE, 1);

    String someRandomTime = "22/11/18 15:45";
    Date d = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(someRandomTime);
    Calendar calendar3 = Calendar.getInstance();
    calendar3.setTime(d);
    calendar3.add(Calendar.DATE, 1);

    Date now = calendar3.getTime();

    Date timestart = calendar1.getTime();
    Date timeend = calendar2.getTime();

    if ((now.after(timestart)) && (now.before(timeend))) {
        Log.d("timetest","add event with name:"+c.getString("name") + " time start: " + timestart+ " time end " + timeend + " time now " + now);
    }
}
catch (ParseException e) {
    e.printStackTrace();
}

最佳答案

您应该使用java.time包中提供的新java日期和时间api。旧的DateCalendar类是flawed和过时的。
如果您只处理时间,那么可以使用LocalTime类来查看某个时间是否介于其他两个时间之间:

// First, parse the times
DateTimeFormatter f = DateTimeFormatter.ofPattern("HH:mm");
LocalTime startTime = LocalTime.parse(c.getString("timestart"), f);
LocalTime endTime = LocalTime.parse(c.getString("timeend"), f);

// Then check if now is between those two times
LocalTime now = LocalTime.now();
if (now.isAfter(startTime) && now.isBefore(endTime)) {
    ...
}

或者,如果活动尚未开始,请告诉人们何时开始:
if (now.isBefore(startTime)) {
    Duration d = Duration.between(now, startTime);
    System.out.println("Event starting in about " + d);
}
else if (now.isBefore(endTime)) {
    System.out.println("Event is now going on");
}
else {
    System.out.println("Event has passed");
}

注意:我假设事件不会每天在指定的时间重复发生,所以您可以考虑不仅存储时间,还存储日期。您可以将时间戳存储为整数,但2018-11-23T11:57:03+01:00等格式也被广泛使用。然后可以使用DateTimeFormatter类轻松地解析它们。

09-11 18:09
查看更多