Java, get days between two dates

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

问题:

In java, I want to get the number of days between two dates, excluding those two dates.

For example:

If first date = 11 November 2011 and the second date = 13 November 2011 then it should be 1.

This is the code I am using but doesn't work (secondDate and firstDate are Calendar objects):

long diff=secondDate.getTimeInMillis()-firstDate.getTimeInMillis(); float day_count=(float)diff / (24 * 60 * 60 * 1000); daysCount.setText((int)day_count+"");                     

I even tried rounding the results but that didn't help.

How do I get the number of days between dates in java excluding the days themselves?

回答1:

I've just tested on SDK 8 (Android 2.2) the following code snippet:

Calendar date1 = Calendar.getInstance(); Calendar date2 = Calendar.getInstance();  date1.clear(); date1.set(    datePicker1.getYear(),    datePicker1.getMonth(),    datePicker1.getDayOfMonth()); date2.clear(); date2.set(    datePicker2.getYear(),    datePicker2.getMonth(),    datePicker2.getDayOfMonth());  long diff = date2.getTimeInMillis() - date1.getTimeInMillis();  float dayCount = (float) diff / (24 * 60 * 60 * 1000);  textView.setText(Long.toString(diff) + " " + (int) dayCount); 

it works perfectly and in both cases (Nov 10,2011 - Nov 8,2011) and (Nov 13,2011 - Nov 11,2011) gives dayCount = 2.0



回答2:

Get Days between java.util.Dates, ignoring daylight savings time

Quick and dirty hack:

public int get_days_between_dates(Date date1, Date date2) {            //if date2 is more in the future than date1 then the result will be negative     //if date1 is more in the future than date2 then the result will be positive.      return (int)((date2.getTime() - date1.getTime()) / (1000*60*60*24l)); } 

This function will work 99.99% of the time except when it surprises you later on in the edge cases during leap-seconds, daylight savings, timezone changes leap years and the like. If you are OK with the calculation being off by 1 (or 2) hours once in a while, this will suffice.

Get Days between Dates taking into account leapseconds, daylight savings, timezones, etc

If you are asking this question you need to slap yourself. What does it mean for two dates to be at least 1 day apart? It's very confusing. What if one Date is midnight in one timezone, and the other date is 1AM in another timezone? Depending on how you interpret it, the answer is both 1 and 0.

You think you can just force the dates you pass into the above function as Universal time format, that will fix some of your problems. But then you just relocate the problem into how you convert your local time to a universal time. The logical conversion from your timezone to universal time may not be what is intuitive. In some cases you will get a day difference when the dates passed in are obviously two days apart.

And you think you can deal with that? There are some retarded Calendar systems in the world which are constantly changing depending on the harvest season and installed political rulers. If you want to convert their time to UTC, java.util.Date is going to fail you at the worst moment.

If you need to calculate the days between dates and it is critical that everything come out right, you need to get an external library called Joda Time: (They have taken care of all the details for you, so you can stay blissfully unaware of them): http://joda-time.sourceforge.net/index.html



回答3:

  1. Don't use floats for integer calculations.

  2. Are you sure your dates are days? The precision of the Date type is milliseconds. So the first thing you need to do is round the date to something which doesn't have hours. Example: It's just one hour from 23:30 2011-11-01 to 00:30 2011-11-02 but the two dates are on different days.



回答4:

If you are only going to be dealing with dates between the years 1900 and 2100, there is a simple calculation which will give you the number of days since 1900:

public static int daysSince1900(Date date) {     Calendar c = new GregorianCalendar();     c.setTime(date);      int year = c.get(Calendar.YEAR);     if (year < 1900 || year > 2099) {         throw new IllegalArgumentException("daysSince1900 - Date must be between 1900 and 2099");     }     year -= 1900;     int month = c.get(Calendar.MONTH) + 1;     int days = c.get(Calendar.DAY_OF_MONTH);      if (month < 3) {         month += 12;         year--;     }     int yearDays = (int) (year * 365.25);     int monthDays = (int) ((month + 1) * 30.61);      return (yearDays + monthDays + days - 63); } 

Thus, to get the difference in days between two dates, you calculate their days since 1900 and calc the difference. Our daysBetween method looks like this:

public static Integer getDaysBetween(Date date1, Date date2) {     if (date1 == null || date2 == null) {         return null;     }      int days1 = daysSince1900(date1);     int days2 = daysSince1900(date2);      if (days1 < days2) {         return days2 - days1;     } else {         return days1 - days2;     } } 

In your case you would need to subtract an extra day (if the days are not equal).

And don't ask me where this calculation came from because we've used it since the early '90s.



回答5:

I have two suggestions:

  1. Make sure your float day_count is calculated correctly

    float day_count = ((float)diff) / (24f * 60f * 60f * 1000f);

  2. If it's rounding error, try using floor method

    daysCount.setText("" + (int)Math.floor(day_count));



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