【问题标题】:Check if time is between two values - always return 0检查时间是否在两个值之间 - 总是返回 0
【发布时间】:2019-10-06 02:03:26
【问题描述】:

如果时间在两个值之间,我有一个显示警报的功能。

例如:

开始 = 07:00:00

结束 = 17:00:00

assert start != null;
                        int from = Integer.valueOf(start.replace(":", ""));
                        assert end != null;
                        int to = Integer.valueOf(end.replace(":", ""));
                        Date date = new Date();
                        Calendar c = Calendar.getInstance();
                        c.setTime(date);
                        int t = c.get(Calendar.HOUR_OF_DAY) * 100 + c.get(Calendar.MINUTE);
                        boolean isBetween = to > from && t >= from && t <= to || to < from && (t >= from || t <= to);
                        a = isBetween ? 1 : 0;

                        if(a == 1) { 
                           alert_f();
                           // a is never 1...
                        }

问题是a 永远不会是 1,即使实际时间介于开始和结束之间。

任何想法为什么?

【问题讨论】:

  • 如果您的时间字符串还包括秒数,您在计算 t 时不应该也包括日历中的秒数吗?
  • @Luksprog 感谢您的回答!如何在我的函数中添加秒数?或从开始/结束变量中删除?你能帮帮我吗?

标签: android time android-calendar datetime-comparison


【解决方案1】:

java.time 和 ThreeTenABP

    String start = "07:00:00";
    String end = "17:00:00";
    LocalTime startTime = LocalTime.parse(start);
    LocalTime endTime = LocalTime.parse(end);

    LocalTime now = LocalTime.now(ZoneId.of("Europe/Rome"));

    boolean isBetween = ! now.isBefore(startTime) && now.isBefore(endTime);
    int a = isBetween ? 1 : 0;
    System.out.println("Is between? " + isBetween + "; a = " + a);

当我刚刚运行它时(罗马 17 点 15 分),我和你一样得到 0,这是意料之中的。如果我指定一个时区还不是下午 5 点,我会得到:

介于两者之间吗?真的; a = 1

因此,您需要为代码选择正确的时区。如果您信任设备时区,您可以使用ZoneId.systemDefault()

在我的布尔表达式中,我使用“not before”来表示“等于或之后”。

我正在利用您的时间字符串采用 ISO 8601 格式这一事实,LocalTime 和其他 java.time 类将其解析(并打印)为默认格式。所以我们不需要显式的格式化程序来解析。

您尝试使用的日期时间类DateCalendar 早已过时且设计不佳。此外,整数值 70 000 和 170 000 作为您的开始和结束时间的表示并没有真正意义。相反,我使用的是现代 Java 日期和时间 API java.time 中的LocalTime。这是一天中没有日期且没有 UTC 偏移量的时间,所以这似乎正是您需要的。

问题:我可以在 Android 上使用 java.time 吗?

是的,java.time 在较旧和较新的 Android 设备上运行良好。它只需要至少 Java 6

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

链接

【讨论】:

    【解决方案2】:

    你可以试试这个方法

        try {
            SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date from = parser.parse("2019-12-31 07:00");
            Date to = parser.parse("2019-12-21 17:00");
            int a;
            Date userDate = parser.parse(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date()));
            if (userDate.after(from) && userDate.before(to)) 
                a = 1;
             else
                a = 0;
        } catch (ParseException e) {
            // Invalid date was entered
        }
    

    【讨论】:

    • 不保证时间会在同一天,例如,如果开始时间是 17:00,结束时间是 7:00,则认为是同一天比较是行不通的.我假设这会检查他的条件检查是否 from 大于 to。
    【解决方案3】:

    好吧,你可以利用 SimpleDateFormat 获取时间,然后将其简化为:

    assert start != null;
    int from = Integer.valueOf(start.replace(":", ""));
    assert end != null;
    int to = Integer.valueOf(end.replace(":", ""));
    // here the change
    int currentTime = Integer.valueOf(new SimpleDateFormat("HH:mm:ss").format(new Date()).replace(":", ""));
    boolean isBetween = to > from && currentTime >= from && currentTime <= to || to < from && (currentTime >= from || currentTime <= to);
    // end
    int a = isBetween ? 1 : 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-16
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 2022-01-20
      • 2017-11-22
      相关资源
      最近更新 更多