Manitoba Manitoba - 5 months ago 15
Android Question

Android Days between two dates

I want to compare two dates for my Android application, but I got a really weird issue.

For example:

If I set the

back in the past
date to 127 days ago:

this.dateEvent = System.currentTimeMillis() - (127 * 24 * 3600 * 1000)


And then compare it to the current date (Days between)

Calendar sDate = getDatePart(new Date(this.dateEvent));
Calendar eDate = getDatePart(new Date(System.currentTimeMillis()));

int daysBetween = 0;
while (sDate.before(eDate))
{
sDate.add(Calendar.DAY_OF_MONTH, 1);
daysBetween ++;
}

while (sDate.after(eDate))
{
eDate.add(Calendar.DAY_OF_MONTH, 1);
daysBetween ++;
}

return daysBetween;


It will return 22 which is not at all what was expected.

Did I make something wrong or is that an issue with the
Calendar
class ?

tpA tpA
Answer

Please refer this code, this may help you

 public String get_count_of_days(String Created_date_String,String Expire_date_String)
      {


          SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy",Locale.getDefault());

            Date Created_convertedDate=null,Expire_CovertedDate=null,todayWithZeroTime=null; 
            try
            {
                Created_convertedDate = dateFormat.parse(Created_date_String);
                Expire_CovertedDate = dateFormat.parse(Expire_date_String);

                Date today = new Date();

                 todayWithZeroTime =dateFormat.parse(dateFormat.format(today));
            } catch (ParseException e)
            {
                e.printStackTrace();
            }


            int c_year=0,c_month=0,c_day=0;

            if(Created_convertedDate.after(todayWithZeroTime))
            {
                Calendar c_cal = Calendar.getInstance();
                c_cal.setTime(Created_convertedDate);

             c_year = c_cal.get(Calendar.YEAR);
             c_month = c_cal.get(Calendar.MONTH);
             c_day = c_cal.get(Calendar.DAY_OF_MONTH);

            }
            else
            {
                Calendar c_cal = Calendar.getInstance();
                c_cal.setTime(todayWithZeroTime);

             c_year = c_cal.get(Calendar.YEAR);
             c_month = c_cal.get(Calendar.MONTH);
             c_day = c_cal.get(Calendar.DAY_OF_MONTH);
            }


            /*Calendar today_cal = Calendar.getInstance();
            int today_year = today_cal.get(Calendar.YEAR);
            int today = today_cal.get(Calendar.MONTH);
            int today_day = today_cal.get(Calendar.DAY_OF_MONTH);
            */





            Calendar e_cal = Calendar.getInstance();
            e_cal.setTime(Expire_CovertedDate);

            int e_year = e_cal.get(Calendar.YEAR);
            int e_month = e_cal.get(Calendar.MONTH);
            int e_day = e_cal.get(Calendar.DAY_OF_MONTH);

            Calendar date1 = Calendar.getInstance();
            Calendar date2 = Calendar.getInstance();

            date1.clear();
            date1.set(c_year, c_month, c_day);
            date2.clear();
            date2.set(e_year, e_month, e_day);

            long diff = date2.getTimeInMillis() - date1.getTimeInMillis();

            float dayCount = (float) diff / (24 * 60 * 60 * 1000);


            return ("" + (int) dayCount + " Days");
      }
Comments