From 59105ca592172cb2d91b0f12b8b3c9fa8c079334 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期二, 22 十月 2024 09:34:28 +0800
Subject: [PATCH] 提交一把
---
server/service/src/main/java/com/doumee/core/utils/DateUtil.java | 144 +++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 135 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..fa297b0 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
@@ -1,11 +1,16 @@
package com.doumee.core.utils;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.ParseException;
import java.text.SimpleDateFormat;
+import java.time.LocalDate;
+import java.time.Period;
+import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
@@ -348,6 +353,8 @@
throw e;
}
}
+
+
/**
* Descrption:鍙栧緱褰撳墠鏃ユ湡,鏍煎紡涓�:yyyy-MM-dd HH:mm:ss
@@ -1298,9 +1305,16 @@
calo.setTime(oldDate);
caln.setTime(newDate);
if(type==0||type==1){
- return calculateDaysBetweenDates(caln, calo) + 1;
+ return calculateDaysBetweenDates(caln, calo) + 1 ;
}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 +2808,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();
- }
- }
/**
*
@@ -2905,6 +2912,7 @@
return nowDateString;
}
+
/**
* @ author zhangyong @ 鑾峰彇褰撴湀鐨勭涓�澶╋紝2009-05-01
*/
@@ -2994,6 +3002,21 @@
return calendar.getTime();
}
+ /**
+ * 鑾峰彇鎸囧畾鏃ユ湡鐨勪笅涓湀绗竴澶�
+ * @param date
+ * @return
+ */
+ public static Date getNextMonthFirst(Date date){
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(date);
+ calendar.add(Calendar.MONTH, 1); // 灏嗘棩鍘嗙殑鏈堜唤澧炲姞1
+ calendar.set(Calendar.DAY_OF_MONTH, 1); // 璁剧疆涓轰笅涓湀鐨勭涓�澶�
+ return calendar.getTime();
+ }
+
+
+
public static String afterDateToStr(Integer days){
Date date = new Date();
@@ -3011,6 +3034,10 @@
public static String getLongDate(Date date) {
return DateToString(date, "yyyyMMddHHmmss");
+ }
+
+ public static String getLongDateTime(Date date) {
+ return DateToString(date, "yyyy-MM-dd HH:mm:ss");
}
public static String DateToString(Date date, String dateStyle) {
@@ -3056,4 +3083,103 @@
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;
+ }
+ }
+
+
+ /**
+ * 鎷兼帴鏃ユ湡鏁版嵁
+ * @param date
+ * @param optType 1:姣忔棩寮�濮� 2锛氭瘡鏃ョ粨鏉� 3:鏄ㄦ棩缁撴潫
+ * @return
+ */
+ public static Date getMontageDate(Date date ,Integer optType) {
+ String strShortDate = DateUtil.dateToString(date,"yyyy-MM-dd");
+ if(optType.equals(1)){
+ return DateUtil.StringToDate(strShortDate +" 00:00:00");
+ }else if(optType.equals(2)){
+ return DateUtil.StringToDate(strShortDate +" 23:59:59");
+ }else if(optType.equals(3)){
+ strShortDate = DateUtil.dateToString(DateUtil.getDateBetween(date,-1),"yyyy-MM-dd");
+ return DateUtil.StringToDate(strShortDate +" 23:59:59");
+ }else{
+ return null;
+ }
+ }
+
+
+ public static void main(String[] args) throws Exception{
+ System.out.println(DateUtil.getLongDateTime(new Date()));;
+ }
+
+ /**
+ * 鑾峰彇鐩稿樊鐨勬湀鏁�
+ * @param sDate
+ * @param eDate
+ * @return
+ */
+ public static Integer getDifferenceMonths(Date sDate ,Date eDate) {
+ LocalDate startDate = sDate.toInstant()
+ .atZone(ZoneId.systemDefault())
+ .toLocalDate();
+ LocalDate endDate = eDate.toInstant()
+ .atZone(ZoneId.systemDefault())
+ .toLocalDate();
+// LocalDate startDate= LocalDate.of(2021,2,28);
+// LocalDate endDate =LocalDate.of(2022,4,5);
+ Period period = Period.between(endDate,startDate);
+ int months = period.getYears()* 12 + period.getMonths();
+ return months + 1;
+ }
+
+
+
}
\ No newline at end of file
--
Gitblit v1.9.3