【问题标题】:How can I compare two times in android? [duplicate]如何在android中比较两次? [复制]
【发布时间】:2018-04-16 08:29:43
【问题描述】:

我想在我的 android 应用程序中比较两次(时间戳)。 这是我检查当前时间是否在两个给定时间之间的函数。

private boolean isBetween(String t1, String t2, String target){
    boolean result = false;
    try{

        Date time1 = new SimpleDateFormat("HH:mm:ss").parse(t1);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(time1);


        Date time2 = new SimpleDateFormat("HH:mm:ss").parse(t2);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(time2);
        calendar2.add(Calendar.DATE, 1);


        Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(d);
        calendar3.add(Calendar.DATE, 1);

        Date x = calendar3.getTime();
        if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
            result = true;
            //checkes whether the current time is between 14:49:00 and 20:11:13.

            //Toast.makeText(UserCheckout.this, "The time is between", Toast.LENGTH_SHORT).show();
        }
    }
    catch (ParseException e){
        result = false;
        Toast.makeText(UserCheckout.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    return result;
}

这是我如何使用这个功能。

if (isBetween("19:00:00", "20:00:00", currentTime)){
    Toast.makeText(this, "Time is  between 7 and 8", Toast.LENGTH_SHORT).show();
}

if (isBetween("20:00:00", "21:00:00", currentTime)){
    Toast.makeText(this, "Time is between 8 and 9", Toast.LENGTH_SHORT).show();
}

但我得到了两个结果。

【问题讨论】:

  • 看看calendar2.add(Calendar.DATE, 1);calendar3.add(Calendar.DATE, 1);,因为我认为您正在检查currentTime 是否在今天19:00 到明天20:00 之间,以及今天20:00 到21 点之间: 00 明天,分别。
  • calendar2.add(Calendar.DATE, 1);calendar3.add(Calendar.DATE, 1);有什么用?您编写的 API 版本是否支持 Java 8?如果是这样,也许使用Java的时间api。
  • @JeroenSteenbeeke 非常感谢你。它奏效了。
  • @M.leRutte 该问题被标记为 Android,因此 Java 8 功能带有一些警告。也就是说,这些 API 可能是解决此问题的更好选择,因此使用这些 API 或 ThreeTen 库确实是一个更好的主意
  • 黑客当然只是使用String.compareTo比较字符串。它的可读性不是很强,但它会起作用。

标签: java android date time


【解决方案1】:

通过调用:

calendar2.add(Calendar.DATE, 1);
calendar3.add(Calendar.DATE, 1);

您正在为这些日期添加一天。您正在检查您的“目标”日期(前移一天)是否在“19:00:00”和“20:00:00(明天)”和“20:00:00”和“21:00”之间: 00(明天)”

将您的代码更改为:

private boolean isBetween(String t1, String t2, String target){
    boolean result = false;
    try{

        Date time1 = new SimpleDateFormat("HH:mm:ss").parse(t1);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(time1);


        Date time2 = new SimpleDateFormat("HH:mm:ss").parse(t2);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(time2);


        Date d = new SimpleDateFormat("HH:mm:ss").parse(target);
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(d);


        Date x = calendar3.getTime();
        if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
            result = true;
            //checkes whether the current time is between 14:49:00 and 20:11:13.

            //Toast.makeText(UserCheckout.this, "The time is between", Toast.LENGTH_SHORT).show();
        }
    }
    catch (ParseException e){
        result = false;
        Toast.makeText(UserCheckout.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    return result;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多