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 ti
You didn't zero the milliseconds :)
try with:
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);
calToday.set(Calendar.MILLISECOND, 0); // this is important
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("-->", "====================");
You are not using Joda-Time, except on the last line.
You are using the old, troublesome, frustrating date-time classes bundled with early versions of Java: java.util.Date/.Calendar. (Well, in Android, an imitation of the old classes -- even more problematic, as there have been some discrepancies.)
Why mix the troublesome old classes with Joda-Time? If you have gone to the bother of adding the Joda-Time jar to your project, stick with Joda-Time all the way.
I strongly recommend that you do indeed learn to use Joda-Time. This work would be much easier, and more accurate.
To count a number of days, it is crucial to consider time zone. Daylight Saving Time (DST) and other anomalies affect the counting of days, and these adjustments vary by time zone.
In the code seen in the Question, the JVM’s current default time zone is implicitly applied to the new DateTime instances. This means this same code may return different results when run on various computers, or when run with different time zone settings. And keep in mind that the current default time zone can be set with a call to TimeZone.setDefault at runtime at any moment by any code in any thread of any app sharing this JVM. Better to specify the expected/intended time zone.
DateTimeZone zone = DateTimeZone.forID( "America/Montral" );
DateTime now = DateTime.now( zone );
DateTime then = now.minusWeeks( 1 ); // Arbitrarily creating a moment in the past.
Interval interval = new Interval( then , now ); // Interval is a pair of moments on the timeline defining a span of time.
int days = Days.daysIn( interval ).getDays(); // First call returns `Days` object. Second call extracts the simple integer number of days as an `int`.
Seems you are trying to get the beginning of the day. You assume that the day begins at the time 00:00:00.000
. That is not always true, because of Daylight Saving Time and possibly other anomalies. Let Joda-Time determine the beginning of the day.
DateTime todayStart = DateTime.now( zone ).withTimeAtStartOfDay();
By the way, in Java 8 and later, Joda-Time is succeeded by the new built-in java.time framework. Joda-Time remains an active supported project though its maintainers recommend programmers move to java.time where possible.