在 Android 中比较日期(时间)时出现问题

Problem comparing dates (times) in Android(在 Android 中比较日期(时间)时出现问题)

本文介绍了在 Android 中比较日期(时间)时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试比较 Android 中的两个日期时遇到问题.我不确定这是模拟器的问题还是我的代码本身有问题.问题是代码在普通的 Java 程序环境中工作,这让我更加困惑.

I am having a problem when i am trying to compare two dates in Android. I am not sure if it is a problem with the emulator or if i have a problem in the code itself. The thing is the code works in a normal Java program environment, which confuses me even more.

我有以下代码来比较 Android 2.1 中的日期:

I have the following code to compare dates in Android 2.1 :

public boolean compareDates(String givenDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    boolean True;
    try {
        True = false;
        Date givenDate = sdf.parse(givenDateString);
        Date currentDate = new Date();

        if(givenDate.after(currentDate)){
            True = true;
        } if(givenDate.before(currentDate)){
            True = false;
        } if(givenDate.equals(currentDate)){
            True = false;
        }   
    } catch (Exception e) {
        Log.e("ERROR! - comparing DATES", e.toString());
    }
    return True;
}

现在代码在 Java 中运行良好,但在 Android 中它一直返回错误.当我用这样的字符串插入 currentDate 变量时,唯一的变化发生了:

now the code works well in Java, but in Android it keeps returning me false. The only change happens when i insert the currentDate variable with a string like this:

Date currentDate = sdf.parse("16:50");

在字符串中设置 currentDate 变量时,当我将它与给定时间之后的值进行比较时,它会返回 true.我也尝试过设置 currentDate 变量:

With the currentDate variable set in a string it returns true when i compare it with a value that's after the time given. I have also tried setting the currentDate variable with:

  Calendar calendar = Calendar.getInstance();
  Date currentDate = calendar.getTime();

我在这里完全不知所措.希望有人对这里可能存在的问题有任何想法.

I am completely at a loss here. Hoping someone has any ideas on what might be a problem here.

--- 编辑---

找到了我的问题的解决方案.我使用日历并从那里读取小时和分钟,然后将它们放在一个字符串中进行解析并且它起作用了.代码现在看起来像这样:

Found the solution for my problem. I used the calendar and read the hour and minute from there, then i put them in a string to parse and it worked. The code now looks like this:

public boolean compareDates(String givenDateString) {
    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    boolean True = false;
    try {
        Date givenDate = sdf.parse(givenDateString);
        Calendar now = Calendar.getInstance();
            int hour = now.get(Calendar.HOUR_OF_DAY);
            int minute = now.get(Calendar.MINUTE);
        Date currentDate = sdf.parse(hour + ":" + minute);

        if(givenDate.after(currentDate)){
            True = true;
        } if(givenDate.before(currentDate)){
            True = false;
        } if(givenDate.equals(currentDate)){
            True = false;
        }   
    } catch (Exception e) {
        Log.e("ERROR! - comparing DATES", e.toString());
    }
    return True;
}

推荐答案

当我对日期进行比较时,我确保使用 Calendar 对象.然后我使用 compareTo() 函数:

When I'm doing comparisons on dates, I make sure to use Calendar objects. Then I use the compareTo() function:

if ( myCalendar.compareTo(upperLimitCalendar) >= 0 )

在此处查看文档:

http://developer.android.com/reference/java/util/日历.html

这篇关于在 Android 中比较日期(时间)时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Android 中比较日期(时间)时出现问题

基础教程推荐