|  |  |  | 
|---|
|  |  |  | 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) ; | 
|---|
|  |  |  | }else if(type==2){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int day = caln.get(Calendar.DATE); | 
|---|
|  |  |  | if(day == 1){ | 
|---|
|  |  |  | return    calculateMonthsBetweenDates(caln, calo) + 1; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | return    calculateMonthsBetweenDates(caln, calo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | }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 sdfLongTimePlus.parse(sdfLongTimePlus.format(date)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | System.out.println(RandomStringUtils.randomNumeric(3)); | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | ex.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * | 
|---|
|  |  |  | 
|---|
|  |  |  | return nowDateString; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * @ author zhangyong @ 获取当月的第一天,2009-05-01 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | cal.add(Calendar.MINUTE,minutes); | 
|---|
|  |  |  | return cal.getTime(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 计算多少自然年/月/日后的日期 | 
|---|
|  |  |  | * @param startDate 开始日期 | 
|---|
|  |  |  | * @param type  0=日;1=月;2=年; | 
|---|
|  |  |  | * @param cycle 周期  X月 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static Date afterDateByType(Date startDate,Integer type,Integer cycle){ | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(startDate); | 
|---|
|  |  |  | if(type==0){ | 
|---|
|  |  |  | calendar.add(Calendar.DATE, cycle); | 
|---|
|  |  |  | }else if(type==1){ | 
|---|
|  |  |  | calendar.add(Calendar.MONTH, cycle); | 
|---|
|  |  |  | }else if(type == 2) { | 
|---|
|  |  |  | calendar.add(Calendar.YEAR, cycle); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return calendar.getTime(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static Integer monthDays(Date date){ | 
|---|
|  |  |  | Calendar calendar = Calendar.getInstance(); | 
|---|
|  |  |  | calendar.setTime(date); | 
|---|
|  |  |  | // 获取当前年份、月份(注意月份从0开始计算) | 
|---|
|  |  |  | int year = calendar.get(Calendar.YEAR); | 
|---|
|  |  |  | int month = calendar.get(Calendar.MONTH) + 1; | 
|---|
|  |  |  | // 根据年份和月份判断闰年情况 | 
|---|
|  |  |  | boolean isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 根据不同月份设定每个月的天数 | 
|---|
|  |  |  | switch (month) { | 
|---|
|  |  |  | case 2: | 
|---|
|  |  |  | if (isLeapYear) { | 
|---|
|  |  |  | return 29; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return 28; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | case 4: | 
|---|
|  |  |  | case 6: | 
|---|
|  |  |  | case 9: | 
|---|
|  |  |  | case 11: | 
|---|
|  |  |  | return 30; | 
|---|
|  |  |  | default: | 
|---|
|  |  |  | return 31; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void main(String[] args) { | 
|---|
|  |  |  | //       System.out.println(DateUtil.DateToStr(DateUtil.afterDateByType( | 
|---|
|  |  |  | //               DateUtil.stringToDate("2024-02-01","yyyy-MM-dd") | 
|---|
|  |  |  | //               ,1,1),"yyyy-MM-dd HH:mm:ss")); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.println( DateUtil.daysBetweenDates(DateUtil.stringToDate("2024-02-29","yyyy-MM-dd"), | 
|---|
|  |  |  | DateUtil.stringToDate("2024-02-01","yyyy-MM-dd")) | 
|---|
|  |  |  | ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | System.out.println(DateUtil.calculateBetween(DateUtil.StringToDate("2024-02-29 23:59:59"),DateUtil.StringToDate("2024-02-01 00:00:00"),0)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //        Calendar calo = Calendar.getInstance(); | 
|---|
|  |  |  | //        Calendar caln = Calendar.getInstance(); // ,"yyyy-MM-dd HH:mm:ss" | 
|---|
|  |  |  | //        caln.setTime(DateUtil.StringToDate("2024-02-29 23:59:59")); | 
|---|
|  |  |  | //        calo.setTime(DateUtil.StringToDate("2024-02-01 00:00:00")); | 
|---|
|  |  |  | //        System.out.println( DateUtil.calculateDaysBetweenDates(calo,caln)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|