两天之间的差异以天为单位

时间:2014-01-06 09:04:01

标签: android date calendar

为什么我会得到以下结果?

today        = 2014-01-06
selected_day = 2014-01-06

结果为0

today        = 2014-01-06
selected_day = 2014-01-07

结果为0

today        = 2014-01-06
selected_day = 2014-01-08

结果是1

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date d1 = df.parse(today);
Date d2 = df.parse(selected_date);
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
long diff = c1.getTimeInMillis() - c2.getTimeInMillis();
System.out.println(diff / (24 * 60 * 60 * 1000));

3 个答案:

答案 0 :(得分:3)

因为您要将两个日期都设置为同一个日历实例

Calendar c2 = Calendar.getInstance();
c1.setTime(d2); // this is wrong :)

将第二个c1更改为c2;)

答案 1 :(得分:1)

你可以这样设置

 cal.set(2008, 8, 1);

像这样使用

long diff = daysBetween(cal1.getTime(),cal2.getTime());

public int daysBetween(Date d1, Date d2){
            return (int)( (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24));
         }

答案 2 :(得分:0)

一开始我误解了确切的问题。现在当我清楚那边时,我想到了一些有关相同的信息。

在考虑日期作为参数时获得差异..尝试这样。

public static int getDaysDifference(Date fromDate,Date toDate)
{
if(fromDate==null||toDate==null)
return 0;

return (int)( (toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24));
}

在考虑日历作为参数时获得差异..尝试这样。

public static int getDaysDifference(Calendar calendar1,Calendar calendar2)
{
if(calendar1==null||calendar2==null)
return 0;

return (int)( (calendar2.getTimeInMillis() - calendar1.getTimeInMillis()) / (1000 * 60 * 60 * 24));
}

简单地将价值传递给它。 :)

现在如果你需要检查一些值的差异而U需要设置一些条件,那么U可能会尝试使用CompareTo函数..

下面提到......

像这样定义你的变量..

java.util.Date d1;
java.util.Date d2;

你的功能就像这样..

public long compareTo(Date d1, Date d2) {
        // TODO Auto-generated method stub
        return d1.getTime() - d2.getTime();

    }

现在当你需要使用它时......试试这个

try{

                        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");

                        try {
                            d1 = sdf.parse(pDisplayDate.getText().toString());
                            d2 = sdf.parse(current_day_validate);
                        } catch (ParseException e1) {
                            // TODO Auto-generated catch block
                            e1.printStackTrace();
                        }

                        /*System.out.println("1. " + sdf.format(d1).toUpperCase());
                        System.out.println("2. " + sdf.format(d2).toUpperCase());*/

                        if(compareTo(d1, d2) >= 0){
                            sm = new SessionManagement(getApplicationContext());

                            if (service_recog_id == 1) {
                                request_url = StaticVars.service_url;
                                new BussinessOwnerHttpAsyncTask().execute();
                            } else if (recognition_id == 3) {
                                request_url = StaticVars.business_owner_url;
                                new BussinessOwnerHttpAsyncTask().execute();
                            } else if (recognition_id == 4) {
                                request_url = StaticVars.service_provider_url;
                                new BussinessOwnerHttpAsyncTask().execute();
                            } else if (recognition_id == 6) {
                                request_url = StaticVars.service_category_url;
                                new BussinessOwnerHttpAsyncTask().execute();
                            }
                        }else{
// Your other stuff
}
}catch(Exception e){
// Your exception
}