我写了一个方法在windows下很正常就是在linux下算出的时间总是变化不定,会差一天的差值.
java 代码
- /**
- * @return 获得两个日期间相差的天数
- */
- public static int getDayRange(Date beginDate, Date endDate)
- {
- if (beginDate == null || endDate == null)
- {
- return 0;
- }
- else
- {
- Calendar calenderBegin = Calendar.getInstance();
- Calendar calenderEnd = Calendar.getInstance();
- calenderBegin.setTime(beginDate);
- calenderEnd.setTime(endDate);
- setTimeToMidnight(calenderBegin);
- setTimeToMidnight(calenderEnd);
- long begin = calenderBegin.getTimeInMillis();
- long end = calenderEnd.getTimeInMillis();
- long intervalMs = end - begin;
- return millisecondsToDays(intervalMs);
- }
- }
- private static void setTimeToMidnight(Calendar calendar)
- {
- calendar.set(Calendar.HOUR_OF_DAY, 0);
- calendar.set(Calendar.MINUTE, 0);
- calendar.set(Calendar.SECOND, 0);
- }
- private static int millisecondsToDays(long intervalMs)
- {
- return (int) (intervalMs / (1000 * 86400));
- }