stefana
stefana

Reputation: 2626

Difference between two dates in days

Why do I get the following outcomes?

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

Outcome is 0

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

Outcome is 0

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

Outcome is 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));

Upvotes: 0

Views: 216

Answers (3)

AndroidHacker
AndroidHacker

Reputation: 3596

At first instance I misunderstood exact problem. Now when I am clear on that side I thought of putting some info regarding same.

Getting difference considering Date as parameter .. try like this.

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

Getting difference considering Calendar as parameter .. try like this.

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

Simply pass value to it. :)

Now if you need to check difference with some values and U need to put some condition then U may try using CompareTo function for that ..

Fallow undermentioned ..

Define your variables like this ..

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

Your function like this ..

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

    }

Now when you need to use this .. try like this

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
}

Upvotes: 0

Sanket Kachhela
Sanket Kachhela

Reputation: 10856

you can set like this way

 cal.set(2008, 8, 1);

use like this way

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

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

Upvotes: 1

Daniel Bo
Daniel Bo

Reputation: 2518

Because you are setting both dates to the same calendar instance

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

change the second c1 to c2 ;)

Upvotes: 3

Related Questions