good morning together,
i developing an android app and i get crazy! since a few days, i try to get the difference days between 2 dates.
i realize it with joda time - this seems to work.
DatePicker datepicker = (DatePicker) findViewById(R.id.Picker); Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, datepicker.getYear()); cal.set(Calendar.MONTH, datepicker.getMonth()); cal.set(Calendar.DATE, datepicker.getDayOfMonth()); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); long sqlDate = cal.getTimeInMillis(); Calendar calToday = Calendar.getInstance(); calToday.set(Calendar.YEAR, calToday.get(Calendar.YEAR)); calToday.set(Calendar.MONTH, calToday.get(Calendar.MONTH)); calToday.set(Calendar.DATE, calToday.get(Calendar.DATE)); calToday.set(Calendar.HOUR_OF_DAY, 0); calToday.set(Calendar.MINUTE, 0); calToday.set(Calendar.SECOND, 0); Date date1 = new Date( datePicker.getYear()-1900, datePicker.getMonth(), datePicker.getDayOfMonth() ); Date today = new Date( calToday.get(Calendar.YEAR)-1900, calToday.get(Calendar.MONTH), calToday.get(Calendar.DAY_OF_MONTH) ); int days = Days.daysBetween(new DateTime(today), new DateTime(date1)).getDays();
i save the long sqlDate in my database. this is the date of date picker in milliseconds.
Now i would like to request this date with an query and calculate the difference days between the date in milliseconds and the date now.
i try this:
Calendar calToday = Calendar.getInstance(); calToday.set(Calendar.YEAR, calToday.get(Calendar.YEAR)); calToday.set(Calendar.MONTH, calToday.get(Calendar.MONTH)); calToday.set(Calendar.DATE, calToday.get(Calendar.DATE)); calToday.set(Calendar.HOUR_OF_DAY, 0); calToday.set(Calendar.MINUTE, 0); calToday.set(Calendar.SECOND, 0); long now = calToday.getTimeInMillis(); long DiffDays = (sqlDate - now) / (24 * 60 * 60 * 1000); Log.e("-->", ""+sqlDate ); Log.e("-->", ""+now); Log.e("-->", ""+(sqlDate - now)); Log.e("-->", ""+DiffDays); Log.e("-->", "====================");
Log Result:
12-02 07:19:09.931 26600-26600/? E/-->: 1451606400515 12-02 07:19:09.931 26600-26600/? E/-->: 1449014400938 12-02 07:19:09.931 26600-26600/? E/-->: 2591999577 12-02 07:19:09.931 26600-26600/? E/-->: 29 12-02 07:19:09.931 26600-26600/? E/-->: ==================== 12-02 07:19:09.931 26600-26600/? E/-->: 1449187200127 12-02 07:19:09.931 26600-26600/? E/-->: 1449014400942 12-02 07:19:09.931 26600-26600/? E/-->: 172799185 12-02 07:19:09.931 26600-26600/? E/-->: 1 12-02 07:19:09.931 26600-26600/? E/-->: ====================
But this is wrong. For Example:
This is the Result of SqlDate - now = 2591999577 (milliseconds)
2591999577 / (24 * 60 * 60 * 1000) = 29,9999 ( 30 days ) NOT 29 Days
and why get i a decimal result - i calculate every time form 00:00:00 to 00:00:00
i hope you can help me :'(