【问题标题】:How can I compare two hours in java?我如何比较java中的两个小时?
【发布时间】:2020-04-12 10:00:05
【问题描述】:

我想计算餐厅的营业时间。我有两个字符串,例如:

String start_hour = "09:00";
String end_hour = "18:00";

例如当前时间:

    Calendar now = Calendar.getInstance();
    String current_hour = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE);

I calculate open hours with this method:

    public boolean isRestaurantOpenNow() {
            try {
                Calendar now = Calendar.getInstance();
                String current_hour = now.get(Calendar.HOUR_OF_DAY) + ":" + now.get(Calendar.MINUTE);
                String st_hour = "09:00";
                String en_hour = "18:00";
                @SuppressLint("SimpleDateFormat") final SimpleDateFormat format = new SimpleDateFormat("HH:mm");
                Date sth = null;
                sth = format.parse(st_hour);
                Date enh = format.parse(en_hour);
                Date nowh = format.parse(current_hour );
                if (nowh != null) {
                    if (nowh.before(enh) && nowh.after(sth)) {
                        // restaurant is open
                        return true;
                    } else {
                        // restaurant is close
                        return false;
                    }
                }
            } catch (ParseException ignored) {
            }
            return false;
        }

但我有一些问题。当 start_hour 为 "13:00" 和 end_hour "05:00" 时,此方法工作错误。因为从第二天 05:00 开始。我该如何解决这个问题?

【问题讨论】:

  • 我建议您不要使用CalendarSimpleDateFormatDate。这些类设计不佳且早已过时,SimpleDateFormat 特别是出了名的麻烦。而是使用来自java.time, the modern Java date and time APILocalTime

标签: java android time datetime-comparison


【解决方案1】:

java.time 和 ThreeTenABP

public boolean isRestaurantOpenNow() {
    LocalTime startHour = LocalTime.parse("13:00");
    LocalTime endHour = LocalTime.parse("05:00");

    LocalTime currentHour = LocalTime.now(ZoneId.systemDefault());
    if (startHour.isBefore(endHour)) { // Both are on the same day
        return currentHour.isAfter(startHour) && currentHour.isBefore(endHour);
    } else { // end is on the next day
        return currentHour.isBefore(endHour) || currentHour.isAfter(startHour) ;
    }
}

刚刚试了一下(我的时区是 21:20):

    System.out.println(isRestaurantOpenNow());

输出是:

是的

问:java.time 不需要 Android API 26 级吗?

java.time 在较旧和较新的 Android 设备上都能很好地工作。它只需要至少 Java 6

  • 在 Java 8 及更高版本以及更新的 Android 设备(从 API 级别 26 起)中,现代 API 是内置的。
  • 在非 Android 的 Java 6 和 7 中,获取 ThreeTen Backport,这是现代类的后向端口(对于 JSR 310,ThreeTen;请参阅底部的链接)。
  • 在(较旧的)Android 上使用 ThreeTen Backport 的 Android 版本。它被称为 ThreeTenABP。并确保从 org.threeten.bp 导入日期和时间类以及子包。

链接

【讨论】:

    【解决方案2】:

    而不是

    nowh.before(enh) && nowh.after(sth)
    

    使用

    nowh.before(enh) && nowh.after(sth) && sth.before(enh)
    || enh.before(sth) && !(nowh.before(enh) && nowh.after(sth))
    

    除此之外,我认为 Calendar 类应该以不同的方式使用...

    【讨论】:

      猜你喜欢
      • 2011-07-21
      • 1970-01-01
      • 1970-01-01
      • 2016-09-07
      • 2012-01-12
      • 2016-05-05
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多