Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 547
Manitoba Avatar asked Apr 27 '14 13:04

Manitoba


People also ask

How can I calculate days between two dates in Mobile?

String dateStr = "2/3/2017"; SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); Date date = sdf. parse(dateStr); Use above template to make your Date object. Then use below code for calculating days in between two dates.

How do you find the difference between two dates in days?

Calculate the no. of days between two dates, divide the time difference of both the dates by no. of milliseconds in a day (1000*60*60*24) Print the final result using document.


2 Answers

Here's a two line solution:

long msDiff = Calendar.getInstance().getTimeInMillis() - testCalendar.getTimeInMillis(); long daysDiff = TimeUnit.MILLISECONDS.toDays(msDiff); 

In this example it gets the number of days between date "testCalendar" and the current date.

like image 106
The Berga Avatar answered Sep 19 '22 11:09

The Berga


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"); } 
like image 35
EminenT Avatar answered Sep 18 '22 11:09

EminenT