| | |
| | | return days; |
| | | } |
| | | |
| | | public static int calculateBetween(Date newDate, Date oldDate,Integer type){ |
| | | Calendar calo = Calendar.getInstance(); |
| | | Calendar caln = Calendar.getInstance(); |
| | | calo.setTime(oldDate); |
| | | caln.setTime(newDate); |
| | | if(type==0||type==1){ |
| | | return calculateDaysBetweenDates(caln, calo) + 1; |
| | | }else if(type==2){ |
| | | return calculateMonthsBetweenDates(caln, calo) + 1; |
| | | }else if(type==3){ |
| | | return calculateYearsBetweenDates(caln, calo) + 1; |
| | | } |
| | | return -1; |
| | | } |
| | | |
| | | |
| | | |
| | | public static int calculateDaysBetweenDates(Calendar startDate, Calendar endDate) { |
| | | long timeDiffInMillis = Math.abs(endDate.getTimeInMillis() - startDate.getTimeInMillis()); |
| | | return (int)(timeDiffInMillis / (24 * 60 * 60 * 1000)); |
| | | } |
| | | |
| | | public static int calculateMonthsBetweenDates(Calendar startDate, Calendar endDate) { |
| | | int yearDiff = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR); |
| | | int monthDiff = endDate.get(Calendar.MONTH) - startDate.get(Calendar.MONTH); |
| | | if (monthDiff < 0 || (monthDiff == 0 && endDate.get(Calendar.DAY_OF_MONTH) < startDate.get(Calendar.DAY_OF_MONTH))) { |
| | | yearDiff--; |
| | | monthDiff += 12; |
| | | } |
| | | return yearDiff * 12 + monthDiff; |
| | | } |
| | | |
| | | public static int calculateYearsBetweenDates(Calendar startDate, Calendar endDate) { |
| | | int yearDiff = endDate.get(Calendar.YEAR) - startDate.get(Calendar.YEAR); |
| | | if (yearDiff > 0 && (startDate.get(Calendar.MONTH) >= endDate.get(Calendar.MONTH) || |
| | | (startDate.get(Calendar.MONTH) == endDate.get(Calendar.MONTH) && startDate.get(Calendar.DAY_OF_MONTH) <= endDate.get(Calendar.DAY_OF_MONTH)))) { |
| | | yearDiff--; |
| | | } else if (yearDiff < 0 && (startDate.get(Calendar.MONTH) <= endDate.get(Calendar.MONTH) || |
| | | (startDate.get(Calendar.MONTH) == endDate.get(Calendar.MONTH) && startDate.get(Calendar.DAY_OF_MONTH) >= endDate.get(Calendar.DAY_OF_MONTH)))) { |
| | | yearDiff++; |
| | | } |
| | | return yearDiff; |
| | | } |
| | | |
| | | |
| | | |
| | | /** |
| | | * 取得与原日期相差一定天数的日期,返回Date型日期 |
| | | * |
| | |
| | | return w; |
| | | } |
| | | |
| | | public static Long twoDaysBetween(Date beginDate, Date endDate) throws ParseException { |
| | | public static Long twoDaysBetween(Date beginDate, Date endDate) { |
| | | long minute = (endDate.getTime() - beginDate.getTime()) / (60 * 1000); |
| | | return minute; |
| | | } |
| | | |
| | | public static Integer daysBetweenDay(Date beginDate, Date endDate) { |
| | | long day = (endDate.getTime() - beginDate.getTime()) / (60 * 1000 * 60 * 24); |
| | | return Math.toIntExact(day); |
| | | } |
| | | |
| | | /** |
| | | * 取两月以前的1好 |
| | | * |