检查时间是否在最小和最大范围内

Check if the time is within the min and max range

我不知道这里发生了什么:如果当前时间在给定范围之间,我需要 return 为真,例如:现在是 15:45:00 并且在 12:00:00 之间(min) 和 18:00:00(max),但是这个方法做错了:

private boolean verifyIfInside(String max, String min){
    try {
        DateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss");

        Date date = new Date();

        String hour1 = min;
        String hour2 = max;
        String newHour = dateFormat.format(date);

        Date minHour, maxHour, nowHour;
        minHour = dateFormat.parse(hora1);
        maxHour = dateFormat.parse(hora2);
        nowHour = dateFormat.parse(newHour );

        if ((minHour.compareTo(nowHour) <= 0) && (maxHour.compareTo(nowHour) >= 0)){
            return true;
        } else {
            return false;
        }
    } catch (ParseException parseException){
        parseException.printStackTrace();
        return false;
    }
}

根据建议的修改,它可以正常工作:

  • 反转最小和最大参数。
  • 使用 .before().after() 比较当前时间是在最小时之后还是最小时之前。
  • 要包含 min/max 时间界限,您可以使用:!nowHour.after(maxHour) && !nowHour.before(minHour);
  • 要排除 min/max 时间界限,请改用:nowHour.after(minHour) && nowHour.before(maxHour);
  • 如果您受限于旧版本的 java,请考虑使用合适的 java.time ThreeTen-Backport 项目,正如@Basil Bourque 所建议的。否则,使用最近的时间API in Java 8+.

代码:

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class scratch_21 {
    public static void main(String[] args) {
        System.out.println(verifyIfInside("14:00:00", "14:15:00"));
    }

    private static boolean verifyIfInside(String min, String max) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

            Date date = new Date();

            String hora1 = min;
            String hora2 = max;
            String newHour = dateFormat.format(date);

            Date minHour, maxHour, nowHour;
            minHour = dateFormat.parse(hora1);
            maxHour = dateFormat.parse(hora2);
            nowHour = dateFormat.parse(newHour);

            return  nowHour.after(minHour) && nowHour.before(maxHour);
        } catch (ParseException parseException) {
            parseException.printStackTrace();
            return false;
        }
    }
}

我已经用 .before().after() 测试了它工作正常你也可以减少代码:-

private static boolean verifyIfInside(String min , String max){
    try {
        DateFormat dateFormat = new SimpleDateFormat ("HH:mm:ss");

        String current_time = dateFormat.format(new Date());

        Date minHour = dateFormat.parse(min);
        Date maxHour = dateFormat.parse(max);
        Date curr_time = dateFormat.parse(current_time);

        return minHour.before(curr_time) && maxHour.after(curr_time);

    } catch (ParseException parseException){
        parseException.printStackTrace();
        return false;
    }
}
  1. 时区很重要。除非您知道当前时间在哪个时区,否则您的验证没有任何意义。为了 reader 的缘故,也请在您的代码中指定它。
  2. 您使用的日期和时间class,DateDateFormatSimpleDateFormat,不仅早已过时,尤其是最后一个也很有名因为工作起来很麻烦。相反,我建议您使用 java.time,现代 Java 日期和时间 API。这甚至提供了 LocalTime class,一天中没有日期的时间,它比 Date.
  3. 更精确地满足您的需求

所以你的方法写得简洁明了:

private static boolean verifyIfInside(String max, String min) {
    LocalTime now = LocalTime.now(ZoneId.of("Europe/Budapest"));
    LocalTime minHour = LocalTime.parse(min);
    LocalTime maxHour = LocalTime.parse(max);
    return ! (now.isBefore(minHour) || now.isAfter(maxHour));
}

如果不是 Europe/Budapest,请替换为您想要的时区。

因为我知道你希望你的范围包含在内,并且由于 isBeforeisAfter 严格表示之前和之后,所以我使用否定来测试时间“不在范围之外”范围”。

我还利用了这样一个事实,即您的时间格式 12:00:00 符合 ISO 8601,现代 classes 将这种格式解析为默认格式,即没有任何明确的格式化程序.一个潜在的缺点是时间字符串的验证较弱:parse() 也将接受 12:0012:23:29.82376342。如果您需要拒绝这些错误,您确实需要一个显式格式化程序。

你的代码出了什么问题?

据我所知,您在问题中的代码工作正常(如果我们只是将 hora1 更改为 hour1 或相反,并且 hora2 也是如此) .您可能对参数的顺序感到困惑。如果 运行 介于 12 和 18 之间,则以下 returns 为真:verifyIfInside("18:00:00", "12:00:00")。然而,许多人会发现将调用写成 verifyIfInside("12:00:00", "18:00:00") 很自然,而 returns 总是错误的。所以几乎可以肯定的是,将方法声明为 verifyIfInside(String min, String max) 会更好,也就是说,在 max.

之前使用 min

当然,正如我提到的,另一种可能性是您的 JVM 使用的时区与您认为它使用的时区不同。这很可能会给您带来意想不到的结果。确保不是这种情况的唯一方法是明确指定应该使用哪个时区。