From 771bf130c7fc34fd710045591080378b54a7e16b Mon Sep 17 00:00:00 2001 From: k94314517 <8417338+k94314517@user.noreply.gitee.com> Date: 星期五, 19 一月 2024 09:27:31 +0800 Subject: [PATCH] 111 --- server/service/src/main/java/com/doumee/core/utils/DateUtil.java | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 52 insertions(+), 1 deletions(-) diff --git a/server/service/src/main/java/com/doumee/core/utils/DateUtil.java b/server/service/src/main/java/com/doumee/core/utils/DateUtil.java index 7942d79..0c1a728 100644 --- a/server/service/src/main/java/com/doumee/core/utils/DateUtil.java +++ b/server/service/src/main/java/com/doumee/core/utils/DateUtil.java @@ -1292,6 +1292,52 @@ 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; + } + + + /** * 鍙栧緱涓庡師鏃ユ湡鐩稿樊涓�瀹氬ぉ鏁扮殑鏃ユ湡锛岃繑鍥濪ate鍨嬫棩鏈� * @@ -2910,11 +2956,16 @@ 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濂� * -- Gitblit v1.9.3