Java,在两个日期之间获取日期
在java中,我想获得两个日期之间的天数,不包括那两个日期。
例如:
如果第一个日期= 11 November 2011
,第二个日期= 13 November 2011
,则应为1
。
这是我正在使用但不起作用的代码( secondDate
和firstDate
是Calendar
对象):
long diff=secondDate.getTimeInMillis()-firstDate.getTimeInMillis(); float day_count=(float)diff / (24 * 60 * 60 * 1000); daysCount.setText((int)day_count+"");
我甚至尝试过舍入结果,但这没有帮助。
如何获取java中日期之间的天数,不包括日期本身?
我刚刚在SDK 8(Android 2.2)上测试了以下代码片段:
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);
它完美无缺,在两种情况下(2011年11月10日 – 2011年11月8日)和(2011年11月13日 – 2011年11月11日)给出dayCount = 2.0
获取java.util.Dates之间的天数,忽略夏令时
快速而肮脏的黑客:
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)); }
此function将在99.99%的时间内工作,除非您在闰秒,夏令时,时区变化闰年等边缘情况下出现意外情况。 如果你可以偶尔计算1(或2)小时的计算,这就足够了。
考虑到闰秒,夏令时,时区等,在日期之间获取天数
如果你问这个问题,你需要打自己。 两个日期相隔至少1天是什么意思? 这很令人困惑。 如果一个日期在一个时区中是午夜,而另一个日期在另一个时区中是凌晨1点怎么办? 根据您的解释方式,答案是1和0。
您认为您可以将传递给上述函数的日期强制为通用时间格式,这将解决您的一些问题。 但是,您只需将问题重新定位为如何将当地时间转换为通用时间。 从您的时区到世界时的逻辑转换可能不是直观的。 在某些情况下,当过去的日期明显相隔两天时,您将获得一天的差异。
而你认为你可以处理这个问题? 世界上有一些迟钝的日历系统根据收获季节不断变化并安装了政治统治者。 如果你想将他们的时间转换为UTC,java.util.Date将在最糟糕的时刻失败。
如果你需要计算日期之间的天数并且一切正常就很关键,你需要得到一个名为Joda Time的外部库:(他们已经为你处理了所有的细节,所以你可以幸福地不知道它们): http : //joda-time.sourceforge.net/index.html
-
不要使用浮点数进行整数计算。
-
你确定你的日期是几天吗?
Date
类型的精度是毫秒。 所以你需要做的第一件事是将日期四舍五入到没有时间的东西。 示例:从23:30 2011-11-01
到00:30 2011-11-02
只有一个小时,但这两个日期是在不同日期。
如果你只是要处理1900年到2100年之间的日期,那么有一个简单的计算可以给你自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); }
因此,为了获得两个日期之间的天数差异,您可以计算自1900年以来的天数并计算差异。 我们的daysBetween方法如下所示:
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; } }
在您的情况下,您需要减去额外的一天(如果天数不相等)。
并且不要问我这个计算的来源,因为我们从90年代初开始使用它。
我有两个建议:
-
确保正确计算
float day_count
float day_count =((float)diff)/(24f * 60f * 60f * 1000f);
-
如果是舍入误差,请尝试使用floor方法
daysCount.setText(“”+(int)Math.floor(day_count));