Search code examples
javaandroiddatecalendardate-difference

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 ?


Solution

  • Please refer this code, this may help you.

    public String getCountOfDays(String createdDateString, String expireDateString) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
    
        Date createdConvertedDate = null, expireCovertedDate = null, todayWithZeroTime = null;
        try {
            createdConvertedDate = dateFormat.parse(createdDateString);
            expireCovertedDate = dateFormat.parse(expireDateString);
    
            Date today = new Date();
    
            todayWithZeroTime = dateFormat.parse(dateFormat.format(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        int cYear = 0, cMonth = 0, cDay = 0;
    
        if (createdConvertedDate.after(todayWithZeroTime)) {
            Calendar cCal = Calendar.getInstance();
            cCal.setTime(createdConvertedDate);
            cYear = cCal.get(Calendar.YEAR);
            cMonth = cCal.get(Calendar.MONTH);
            cDay = cCal.get(Calendar.DAY_OF_MONTH);
    
        } else {
            Calendar cCal = Calendar.getInstance();
            cCal.setTime(todayWithZeroTime);
            cYear = cCal.get(Calendar.YEAR);
            cMonth = cCal.get(Calendar.MONTH);
            cDay = cCal.get(Calendar.DAY_OF_MONTH);
        }
    
    
        /*Calendar todayCal = Calendar.getInstance();
        int todayYear = todayCal.get(Calendar.YEAR);
        int today = todayCal.get(Calendar.MONTH);
        int todayDay = todayCal.get(Calendar.DAY_OF_MONTH);
        */
    
        Calendar eCal = Calendar.getInstance();
        eCal.setTime(expireCovertedDate);
    
        int eYear = eCal.get(Calendar.YEAR);
        int eMonth = eCal.get(Calendar.MONTH);
        int eDay = eCal.get(Calendar.DAY_OF_MONTH);
    
        Calendar date1 = Calendar.getInstance();
        Calendar date2 = Calendar.getInstance();
    
        date1.clear();
        date1.set(cYear, cMonth, cDay);
        date2.clear();
        date2.set(eYear, eMonth, eDay);
    
        long diff = date2.getTimeInMillis() - date1.getTimeInMillis();
    
        float dayCount = (float) diff / (24 * 60 * 60 * 1000);
    
        return ("" + (int) dayCount + " Days");
    }