比较问题日期(次)Android中 [英] Problem comparing dates (times) in Android

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

问题描述

我有一个问题,当我试图在Android中比较两个日期。我不知道这是否是与模拟器了问题,如果我有在C本身的$ C $的一个问题。 事情是在一个普通的Java程序环境,这混淆了我,甚至更多code ++工程。

我有以下的code到Android 2.1的比较日期:

 公共布尔compareDates(字符串givenDateString){
    SimpleDateFormat的SDF =新的SimpleDateFormat(HH:MM);
    布尔值true;
    尝试 {
        真= FALSE;
        日期givenDate = sdf.parse(givenDateString);
        日期的currentdate =新的日期();

        如果(givenDate.after(的currentdate)){
            真=真实;
        }如果(givenDate.before(的currentdate)){
            真= FALSE;
        }如果(givenDate.equals(的currentdate)){
            真= FALSE;
        }
    }赶上(例外五){
        Log.e(错误 - 比较日期!,e.toString());
    }
    返回True;
}
 

现在的code运作良好的Java,但是在Android中它保持返回我的错误。 唯一的变化发生在我插入的currentdate变量,一个字符串是这样的:

 日期的currentdate = sdf.parse(16:50);
 

随着的currentdate变量设置字符串中的它,当我有一个值是给定的时间之后进行比较返回true。我也试图与设定的currentdate变量:

 台历挂历= Calendar.getInstance();
  日期的currentdate = calendar.getTime();
 

我完全在这里的损失。希望有人有什么可能是一个问题,在这里的任何想法。

---编辑---

发现我的问题的解决方案。我使用的日历,并从那里读小时和分钟,然后我把它们放在一个字符串解析和它的工作。在code现在看起来是这样的:

 公共布尔compareDates(字符串givenDateString){
    SimpleDateFormat的SDF =新的SimpleDateFormat(HH:MM);
    布尔真= FALSE;
    尝试 {
        日期givenDate = sdf.parse(givenDateString);
        日历现在= Calendar.getInstance();
            INT小时= now.get(Calendar.HOUR_OF_DAY);
            INT分钟= now.get(Calendar.MINUTE);
        日期的currentdate = sdf.parse(小时+:+分钟);

        如果(givenDate.after(的currentdate)){
            真=真实;
        }如果(givenDate.before(的currentdate)){
            真= FALSE;
        }如果(givenDate.equals(的currentdate)){
            真= FALSE;
        }
    }赶上(例外五){
        Log.e(错误 - 比较日期!,e.toString());
    }
    返回True;
}
 

解决方案

在我做的日期比较,我一定要使用日历对象。然后,我用的compareTo()函数:

 如果(myCalendar.compareTo(upperLimitCalendar)> = 0)
 

在这里查看文档:

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

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

解决方案

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

这篇关于比较问题日期(次)Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆