Android 使用时区检查打开和关闭之间的时间是否已经过去

Android check if between open and closing time has elapsed using timezone

如何在 android 中检查两次之间的时间? 我有开门时间和关门时间,我想使用特定国家时区检查关门时间是否已过。

 public void timeElapse(String start, String end, String tz){
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm aa", Locale.getDefault());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm\aa", Locale.getDefault());
        try {
            sdf.setTimeZone(tzs);
            Date currentTimeDate = sdf.parse(start);
            Date endTimeDate = sdf.parse(end);
            int a = currentTimeDate.compareTo(endTimeDate); // false / current time has not passed end time.
            int b = endTimeDate.compareTo(currentTimeDate); // true / end time has passed current time.

            Log.e("timeElapse", "("+end+") A = " + a + " B = " + b);
        } catch (ParseException ignored) {

        }
    }

第二次尝试

public void timeElapse(String start, String end, String tz){
    SimpleDateFormat sdf = new SimpleDateFormat("hh:mm\aa", Locale.getDefault());
    try {
        TimeZone tzs = TimeZone.getTimeZone(tz);
        sdf.setTimeZone(tzs);
        long currentTimeDate = sdf.parse(start).getTime();
        long endTimeDate = sdf.parse(end).getTime();
        if(currentTimeDate>endTimeDate){
            Log.e("timeElapse", "Time has passed");
        }else{
            Log.e("timeElapse", "You are on time");
        }
    } catch (ParseException ignored) {

    }
}

用法

timeElapse("10:00AM", "10:00PM", "Asia/Kuala_Lumpur");
timeElapse("10:00AM", "12:00PM", "Asia/Kuala_Lumpur");
timeElapse("11:30AM", "05:00PM", "Asia/Kuala_Lumpur");
timeElapse("10:00AM", "01:00AM", "Asia/Kuala_Lumpur");

我的代码不起作用。我收到错误 java.text.ParseException:无法解析的日期:“10:00AM”

java.time 通过脱糖

考虑使用 java.time,现代 Java 日期和时间 API,作为您的工作时间。

在更仔细地阅读问题后对代码进行了编辑。

private static final DateTimeFormatter TIME_PARSER
        = DateTimeFormatter.ofPattern("hh:mma", Locale.ENGLISH);

public static void timeElapse(String start, String end) {
    LocalTime currentTime = LocalTime.parse(start, TIME_PARSER);
    LocalTime endTime = LocalTime.parse(end, TIME_PARSER);
    if (currentTime.isAfter(endTime)) {
        System.out.println("Time has passed");
    } else {
        System.out.println("You are on time");
    }
}

代码是不是一目了然?这是 java.time 我觉得不错的一件事。并且在评论中已经暗示,我们不需要时区(极端情况除外)。所以我省略了第三个参数。让我们用你的例子试试看:

    timeElapse("10:00AM", "10:00PM");
    timeElapse("10:00AM", "12:00PM");
    timeElapse("11:30AM", "05:00PM");
    timeElapse("10:00AM", "01:00AM");

输出为:

You are on time
You are on time
You are on time
Time has passed

时区不重要吗?如果您的时间落在 后退 中,时钟向后转动并且相同的时钟时间重复,我们将无法判断哪个更早。我上面的代码没有考虑这种特殊情况,为此,我们还需要知道日期。

原码

private static final DateTimeFormatter TIME_PARSER
        = DateTimeFormatter.ofPattern("hh:mma", Locale.ENGLISH);

public static void timeElapse(String start, String end, String tz){
    LocalTime startTime = LocalTime.parse(start, TIME_PARSER);
    LocalTime endTime = LocalTime.parse(end, TIME_PARSER);
    LocalTime now = LocalTime.now(ZoneId.of(tz));
    if (now.isBefore(startTime)) {
        System.out.println("Not open yet");
    } else if (now.isAfter(endTime)) {
        System.out.println("Time has passed");
    } else {
        System.out.println("You are on time");
    }
}

代码是不是一目了然?这是 java.time 我觉得不错的一件事。让我们用你的例子试试看:

    timeElapse("10:00AM", "10:00PM", "Asia/Kuala_Lumpur");
    timeElapse("10:00AM", "12:00PM", "Asia/Kuala_Lumpur");
    timeElapse("11:30AM", "05:00PM", "Asia/Kuala_Lumpur");
    timeElapse("10:00AM", "01:00AM", "Asia/Kuala_Lumpur");

我 运行 这个代码在马来西亚的 2:15 上午附近,所以正如预期的那样,输出有点沉闷:

Not open yet
Not open yet
Not open yet
Not open yet

我们也试试:

    timeElapse("12:00AM", "03:00AM", "Asia/Kuala_Lumpur");
    timeElapse("01:00AM", "02:00AM", "Asia/Kuala_Lumpur");
You are on time
Time has passed

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

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

  • 在 Java 8 和更新的 Android 设备上(从 API 级别 26)内置现代 API。
  • 在非 Android Java 6 和 7 中获得 ThreeTen Backport,现代 类 的 backport(ThreeTen 用于 JSR 310;请参阅底部的链接)。
  • 在较旧的 Android 上使用脱糖或 ThreeTen Backport 的 Android 版本。它叫做 ThreeTenABP。在后一种情况下,请确保使用子包从 org.threeten.bp 导入日期和时间 类。

你的代码出了什么问题?

您的格式模式字符串中的分钟和 AM/PM 标记之间有一个反斜杠:"hh:mm\aa"。这需要在您的时间字符串中使用反斜杠,但它们类似于 "10:00AM",即没有任何反斜杠。我相信这导致了您的 ParseException。为格式化程序指定 Locale.getDefault() 也可能存在问题。 AM 和 PM 几乎不用于英语以外的其他语言,但在某些语言中确实有其他名称(文本)。您要求文本使用您的 JVM(和您的设备)的默认语言。

另请注意,如果您想将解析时间与现在的时间进行比较,使用 java.time 很容易。它不适用于 DateSimpleDateFormatSimpleDateFormat 将每个字符串解析为 1970 年 1 月 1 日的指定时间,因此与现在的时间比较没有意义。

链接