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

发布于 2024-11-19 09:57:57 字数 2024 浏览 0 评论 0原文

当我尝试在 Android 中比较两个日期时遇到问题。我不确定这是模拟器的问题还是代码本身的问题。 问题是代码可以在正常的 Java 程序环境中运行,这让我更加困惑。

我有以下代码来比较 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 中它总是返回 false。 当我插入带有这样的字符串的 currentDate 变量时,唯一的变化发生:

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

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

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

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

--- 编辑 ---

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

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;
}

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.

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;
}

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");

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.

--- EDIT ---

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里泪两行 2024-11-26 09:57:57

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

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

请参阅此处的文档:

http://developer .android.com/reference/java/util/Calendar.html

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 )

See the documentation here:

http://developer.android.com/reference/java/util/Calendar.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文