Wrong count of difference days between 2 dates with joda time?

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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 :'(

回答1:

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("-->", "===================="); 


回答2:

Joda-Time?

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.

Time Zone

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`. 

First Moment Of The Day

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(); 

java.time

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!