From c9df4cfec262ca54af52715af60a1276c37d08c5 Mon Sep 17 00:00:00 2001 From: k94314517 <8417338+k94314517@user.noreply.gitee.com> Date: 星期一, 29 一月 2024 20:12:24 +0800 Subject: [PATCH] 111 --- server/service/src/main/java/com/doumee/core/utils/DateUtil.java | 79 +++++++++++++++++++++++++++++++++++---- 1 files changed, 70 insertions(+), 9 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 0c1a728..45bdcc0 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 @@ -1298,9 +1298,16 @@ calo.setTime(oldDate); caln.setTime(newDate); if(type==0||type==1){ - return calculateDaysBetweenDates(caln, calo) + 1; + return calculateDaysBetweenDates(caln, calo) ; }else if(type==2){ - return calculateMonthsBetweenDates(caln, calo) + 1; + + 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; } @@ -2794,13 +2801,6 @@ return sdfLongTimePlus.parse(sdfLongTimePlus.format(date)); } - public static void main(String[] args) { - try { - System.out.println(RandomStringUtils.randomNumeric(3)); - } catch (Exception ex) { - ex.printStackTrace(); - } - } /** * @@ -2904,6 +2904,7 @@ String nowDateString = dateTypeToString(new Date(), fFormatStr); return nowDateString; } + /** * @ author zhangyong @ 鑾峰彇褰撴湀鐨勭涓�澶╋紝2009-05-01 @@ -3056,4 +3057,64 @@ 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")); + } + + } \ No newline at end of file -- Gitblit v1.9.3