【问题标题】:Android Java If Time Between Two Times ErrorAndroid Java 如果两次之间的时间错误
【发布时间】:2018-11-22 15:56:58
【问题描述】:

我正在使用 JSON 文件来存储会议的多个条目。我正在尝试在 Java 的 Calendar 对象中获取使用 date.before 和 date.after 的当前事件。我当前的代码正在返回三个不同的事件,我不知道,我想知道有人对我尝试过的所有事情有任何建议。我将在下面附上 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

JSON

{
      "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"
    },

Java

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

【问题讨论】:

  • 1.尝试记录日历对象以查看时间是否正确解析。 2.您也可以直接比较日历对象,而不是转换为日期。
  • 我确实做了一些日志,我附上了@viv
  • 你到底想做什么?
  • 您不应再使用DateCalendar 类。使用它们很麻烦。请改用新的 Java 日期和时间 API。如果由于您的 Android 版本而无法使用,您可以使用 ThreeTen Backport。
  • 这可能不是 OP 当前编写的此类代码的最大问题,但如果您要构建更大一点的应用程序,它可能会节省您的时间。 Here's a list of issues with these classes.

标签: java android simpledateformat android-calendar


【解决方案1】:

就像我在 cmets 部分提到的:

您可以将时间转换为毫秒,然后检查当前时间是否大于事件的开始时间。如果是,您将返回数据。


像这样:

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

    //Convert event time to milliseconds
    int timestart = calendar1.get(Calendar.MILLISECOND);

    //Get current time in milliseconds
    Calendar calendarCurrent = Calendar.getInstance();
    calendarCurrent.setTime(new Date());
    int now = calendar.get(Calendar.MILLISECOND);


    //Check if the event time is greater then the current time
    if (timestart > now) {
        Log.d("timetest - ",+"Event "+c.getString("name")+" already started");
    }else{
        Log.d("timetest - ",+"Event "+c.getString("name")+" has not started yet");
    }
 } catch (ParseException e) {
     e.printStackTrace();
}

重要提示:

我还建议将事件的日期和时间以毫秒为单位存储在您的数据库中,而不是对其进行硬编码。在我的回答中,我这样做是为了演示目的。

如果您将开始时间和结束时间存储为毫秒,您将能够确定距离活动开始的剩余时间,例如 this

【讨论】:

  • 我试过这段代码,但它不起作用。当我控制台日志它说开始是 0?
  • 这不是让已经很复杂的事情变得更加复杂吗?
  • @OleV.V.我觉得它一点也不复杂?
  • 您可以与其他答案进行比较,用 9 行而不是 20 行完成相同的任务。还发现这 9 行更易于阅读。
  • 这个答案根本不起作用。 Calendar.MILLISECOND 字段返回秒内的毫秒数,范围始终为 0 到 999。
【解决方案2】:

您应该使用 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 类轻松解析它们。

【讨论】:

  • @CurtisB 查看编辑:只需使用LocalTime now = LocalTime.now()
  • 我建议您将时区传递给LocalTme.now,这样即使您只是传递ZoneId.systemDefault(),您也可以知道得到了什么。由于来自 JSON 的时间(如 15:30)符合 ISO 8601 格式,因此如果您不希望指定用于解析它们的显式格式化程序,则无需指定。很好的答案来证明使用 java.time 的工作是多么简单。
猜你喜欢
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
相关资源
最近更新 更多