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 ?
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.
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.
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.
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"); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With