From dd6687b118561100e1677e88a9c2f5842a54c531 Mon Sep 17 00:00:00 2001
From: jialh <1972868360@qq.com>
Date: 星期四, 16 四月 2026 18:14:14 +0800
Subject: [PATCH] 水电话费
---
java110-utils/src/main/java/com/java110/utils/util/DateUtil.java | 606 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 597 insertions(+), 9 deletions(-)
diff --git a/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java b/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
index b5ff030..b615608 100755
--- a/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
+++ b/java110-utils/src/main/java/com/java110/utils/util/DateUtil.java
@@ -1,27 +1,55 @@
package com.java110.utils.util;
+import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
-import java.util.Calendar;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
+import java.time.Duration;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.Period;
+import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
+import java.util.*;
/**
* Created by wuxw on 2017/7/24.
*/
public class DateUtil {
+ public static final String DATE_FORMATE_STRING_MILLI = "yyyyMMddHHmmssSSS"; // 17浣嶏細骞存湀鏃ユ椂鍒嗙姣
private static DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
- public static final String LAST_TIME = "2038-01-01 00:00:00";
+ public static final String LAST_TIME = "2050-01-01 00:00:00";
+ public static int getTotalDaysInMonth(int year, int month) {
+ LocalDate date = LocalDate.of(year, month, 1); // 鍒涘缓涓�涓棩鏈熷璞★紝骞翠唤鍜屾湀浠戒负鎸囧畾鍊硷紝鏃ユ湡涓�1鍙�
+ return date.lengthOfMonth(); // 杩斿洖璇ユ湀鐨勫ぉ鏁�
+ }
+ private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+
+ public static boolean isValidDate(String dateStr) {
+ // 鉁� 鍏抽敭淇锛氬厛褰诲簳鍘婚櫎鍓嶅悗绌虹櫧锛堝寘鎷┖鏍笺�佸埗琛ㄧ銆佹崲琛岋級
+ if (dateStr == null || dateStr.isEmpty()) {
+ return true;
+ }
+
+ // 鍘绘帀鎵�鏈夌┖鐧藉悗鍐嶆牎楠�
+ String cleanDate = dateStr.trim();
+
+ try {
+ LocalDate.parse(cleanDate, DATE_FORMATTER);
+ return true;
+ } catch (DateTimeParseException e) {
+ return false;
+ }
+ }
private static Map<String, SimpleDateFormat> formats = new HashMap();
public static final String DATE_FORMATE_STRING_DEFAULT = "yyyyMMddHHmmss";
public static final String DATE_FORMATE_STRING_A = "yyyy-MM-dd HH:mm:ss";
public static final String DATE_FORMATE_STRING_B = "yyyy-MM-dd";
+ public static final String DATE_FORMATE_STRING_SLASH_NON_ZERO = "yyyy/M/d";
public static final String DATE_FORMATE_STRING_C = "MM/dd/yyyy HH:mm:ss a";
public static final String DATE_FORMATE_STRING_D = "yyyy-MM-dd HH:mm:ss a";
public static final String DATE_FORMATE_STRING_E = "yyyy-MM-dd'T'HH:mm:ss'Z'";
@@ -32,11 +60,18 @@
public static final String DATE_FORMATE_STRING_J = "yyyyMMddHHmmss.SSS";
public static final String DATE_FORMATE_STRING_K = "yyyyMMddHHmmssSSS";
public static final String DATE_FORMATE_STRING_L = "MMdd";
+ public static final String DATE_FORMATE_STRING_M = "yyyyMM";
+ public static final String DATE_FORMATE_STRING_N = "HHmmss";
+ public static final String DATE_FORMATE_STRING_O = "yyyyMMddHHmm";
+ public static final String DATE_FORMATE_STRING_Q = "yyyy-MM";
+ private static final DateTimeFormatter DEFAULT_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+
static {
formats.put("yyyyMMddHHmmss", new SimpleDateFormat("yyyyMMddHHmmss"));
formats.put("yyyy-MM-dd HH:mm:ss", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
formats.put("yyyy-MM-dd", new SimpleDateFormat("yyyy-MM-dd"));
+ formats.put("yyyy/MM/dd", new SimpleDateFormat("yyyy/M/d"));
formats.put("MM/dd/yyyy HH:mm:ss a", new SimpleDateFormat("MM/dd/yyyy HH:mm:ss a"));
formats.put("yyyy-MM-dd HH:mm:ss a", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a"));
formats.put("yyyy-MM-dd'T'HH:mm:ss'Z'", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
@@ -47,6 +82,11 @@
formats.put("yyyyMMddHHmmss.SSS", new SimpleDateFormat("yyyyMMddHHmmss.SSS"));
formats.put("yyyyMMddHHmmssSSS", new SimpleDateFormat("yyyyMMddHHmmssSSS"));
formats.put("MMdd", new SimpleDateFormat("MMdd"));
+ formats.put("yyyyMM", new SimpleDateFormat("yyyyMM"));
+ formats.put("HHmmss", new SimpleDateFormat("HHmmss"));
+ formats.put("yyyyMMddHHmm", new SimpleDateFormat("yyyyMMddHHmm"));
+ formats.put("yyyy-MM", new SimpleDateFormat("yyyy-MM"));
+
}
@@ -115,6 +155,22 @@
}
}
+ public static String getFormatTimeStringA(Date date) {
+ SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_A);
+
+ synchronized (sDateFormat) {
+ return sDateFormat.format(date);
+ }
+ }
+
+ public static String getFormatTimeStringB(Date date) {
+ SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_B);
+
+ synchronized (sDateFormat) {
+ return sDateFormat.format(date);
+ }
+ }
+
public static String getDefaultFormateTimeString(Date date) {
return getFormatTimeString(date, "yyyyMMddHHmmss");
}
@@ -137,6 +193,29 @@
}
}
+ public static Date getDateFromStringB(String date) {
+ SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_B);
+ try {
+ synchronized (sDateFormat) {
+ return sDateFormat.parse(date);
+ }
+ } catch (Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+
+ public static Date getDateFromStringA(String date) {
+ SimpleDateFormat sDateFormat = getDateFormat(DateUtil.DATE_FORMATE_STRING_A);
+ try {
+ synchronized (sDateFormat) {
+ return sDateFormat.parse(date);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IllegalArgumentException(e);
+ }
+ }
+
public static Date getDefaultDateFromString(String date)
throws ParseException {
return getDateFromString(date, "yyyyMMddHHmmss");
@@ -149,6 +228,7 @@
public static String getNow(String pattern) {
return getFormatTimeString(new Date(), pattern);
}
+
public static String getLastTime() {
return LAST_TIME;
@@ -350,15 +430,66 @@
public static Date getNextMonthFirstDate() {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_MONTH, 1);
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
+ calendar.set(Calendar.MINUTE, 0);
+ calendar.set(Calendar.SECOND, 0);
calendar.add(Calendar.MONTH, 1);
return calendar.getTime();
}
- public static Date getFirstDate() {
- Calendar calendar = Calendar.getInstance();
- calendar.set(Calendar.DAY_OF_MONTH, 1);
- return calendar.getTime();
+ public static Date getNextMonthFirstDate(String curDate) {
+ Date date = DateUtil.getDateFromStringB(curDate);
+ return getNextMonthFirstDate(date);
}
+
+ public static Date getNextMonthFirstDate(Date curDate) {
+
+ return getNextMonthFirstDate(curDate, 1);
+ }
+
+ public static Date getNextMonthFirstDate(Date curDate, int monthCount) {
+
+ Calendar curDateCal = Calendar.getInstance();
+ curDateCal.setTime(curDate);
+ curDateCal.set(Calendar.DAY_OF_MONTH, 1);
+ curDateCal.set(Calendar.HOUR_OF_DAY, 0);
+ curDateCal.set(Calendar.MINUTE, 0);
+ curDateCal.set(Calendar.SECOND, 0);
+ curDateCal.add(Calendar.MONTH, monthCount);
+ return curDateCal.getTime();
+ }
+
+ public static Date getFirstDate() {
+ Calendar curDateCal = Calendar.getInstance();
+ curDateCal.set(Calendar.DAY_OF_MONTH, 1);
+ curDateCal.set(Calendar.HOUR_OF_DAY, 0);
+ curDateCal.set(Calendar.MINUTE, 0);
+ curDateCal.set(Calendar.SECOND, 0);
+ Date curDate = curDateCal.getTime();
+ return curDate;
+ }
+
+ public static Date getFirstDate(Date curDate) {
+ Calendar curDateCal = Calendar.getInstance();
+ curDateCal.setTime(curDate);
+ curDateCal.set(Calendar.DAY_OF_MONTH, 1);
+ curDateCal.set(Calendar.HOUR_OF_DAY, 0);
+ curDateCal.set(Calendar.MINUTE, 0);
+ curDateCal.set(Calendar.SECOND, 0);
+ return curDateCal.getTime();
+ }
+
+ public static Date getFirstDate(String curDate) {
+ Date date = DateUtil.getDateFromStringB(curDate);
+ Calendar curDateCal = Calendar.getInstance();
+ curDateCal.setTime(date);
+ curDateCal.set(Calendar.DAY_OF_MONTH, 1);
+ curDateCal.set(Calendar.HOUR_OF_DAY, 0);
+ curDateCal.set(Calendar.MINUTE, 0);
+ curDateCal.set(Calendar.SECOND, 0);
+ return curDateCal.getTime();
+ }
+
public static String getNextMonthFirstDay(String fmt) {
String returndate = "";
@@ -405,6 +536,55 @@
return maxDate;
}
+ public static int getMonthDay(Date date) {
+ Calendar a = Calendar.getInstance();
+ a.setTime(date);
+ return a.getActualMaximum(Calendar.DAY_OF_MONTH);
+ }
+
+
+
+ public static String getAddDayString(Date date, String pattern, int days) {
+ SimpleDateFormat sf = new SimpleDateFormat(pattern);
+ Calendar c = Calendar.getInstance();
+ c.setTime(date);
+ c.add(Calendar.DAY_OF_MONTH, days);
+ return sf.format(c.getTime());
+ }
+
+ public static String getAddDayStringB(Date date, int days) {
+ SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_B);
+ Calendar c = Calendar.getInstance();
+ c.setTime(date);
+ c.add(Calendar.DAY_OF_MONTH, days);
+ return sf.format(c.getTime());
+ }
+
+ public static String getAddDayStringA(Date date, int days) {
+ SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
+ Calendar c = Calendar.getInstance();
+ c.setTime(date);
+ c.add(Calendar.DAY_OF_MONTH, days);
+ return sf.format(c.getTime());
+ }
+
+
+ public static String getAddHoursStringA(Date date, int hours) {
+ SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
+ Calendar c = Calendar.getInstance();
+ c.setTime(date);
+ c.add(Calendar.HOUR_OF_DAY, hours);
+ return sf.format(c.getTime());
+ }
+
+
+ public static String getAddMonthStringA(Date date, int month) {
+ SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMATE_STRING_A);
+ Calendar c = Calendar.getInstance();
+ c.setTime(date);
+ c.add(Calendar.MONTH, month);
+ return sf.format(c.getTime());
+ }
/**
* 鍦ㄧ粰瀹氱殑鏃ユ湡鍔犱笂鎴栧噺鍘绘寚瀹氭湀浠藉悗鐨勬棩鏈�
@@ -418,6 +598,20 @@
c.setTime(sourceDate);
c.add(Calendar.MONTH, month);
+ return c.getTime();
+ }
+
+ /**
+ * 鍦ㄧ粰瀹氱殑鏃ユ湡鍔犱笂鎴栧噺鍘绘寚瀹氬ぉ鏁板悗鐨勬棩鏈�
+ *
+ * @param sourceDate 鍘熷鏃堕棿
+ * @param day 瑕佽皟鏁寸殑鏈堜唤锛屽悜鍓嶄负璐熸暟锛屽悜鍚庝负姝f暟
+ * @return
+ */
+ public static Date stepDay(Date sourceDate, int day) {
+ Calendar c = Calendar.getInstance();
+ c.setTime(sourceDate);
+ c.add(Calendar.DATE, day);
return c.getTime();
}
@@ -497,4 +691,398 @@
private static int millisecondsToDays(long intervalMs) {
return (int) (intervalMs / (1000 * 86400));
}
+
+ /**
+ * 銆�銆� *瀛楃涓茬殑鏃ユ湡鏍煎紡鐨勮绠�
+ */
+ public static int daysBetween(String smdate, String bdate) {
+ long between_days = 0;
+ try {
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
+ Calendar cal = Calendar.getInstance();
+ cal.setTime(sdf.parse(smdate));
+ long time1 = cal.getTimeInMillis();
+ cal.setTime(sdf.parse(bdate));
+ long time2 = cal.getTimeInMillis();
+ between_days = (time2 - time1) / (1000 * 3600 * 24);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+
+ return Integer.parseInt(String.valueOf(between_days));
+ }
+
+// public static double dayCompare(Date fromDate, Date toDate) {
+// double resMonth = 0.0;
+// Calendar from = Calendar.getInstance();
+// from.setTime(fromDate);
+// Calendar to = Calendar.getInstance();
+// to.setTime(toDate);
+// //姣旇緝鏈堜唤宸� 鍙兘鏈夋暣鏁� 涔熶細璐熸暟
+// int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
+// //姣旇緝骞村樊
+// int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
+//
+// //鐪熷疄 鐩稿樊鏈堜唤
+// result = result + month;
+//
+// //寮�濮嬫椂闂� 2021-06-01 2021-08-05 result = 2 2021-08-01
+// Calendar newFrom = Calendar.getInstance();
+// newFrom.setTime(fromDate);
+// newFrom.add(Calendar.MONTH, result);
+// //濡傛灉鍔犳湀浠藉悗 澶т簬浜嗗綋鍓嶆椂闂� 榛樿鍔� 鏈堜唤 -1 鎯呭喌 12-19 21-01-10
+// //杩欎釜鏄鐨勯�昏緫涓�瀹氬ソ濂界悊瑙�
+// if (newFrom.getTime().getTime() > toDate.getTime()) {
+// newFrom.setTime(fromDate);
+// result = result - 1;
+// newFrom.add(Calendar.MONTH, result);
+// }
+//
+// // t1 2021-08-01 t2 2021-08-05
+// long t1 = newFrom.getTime().getTime();
+// long t2 = to.getTime().getTime();
+// //鐩稿樊姣
+// double days = (t2 - t1) * 1.00 / (24 * 60 * 60 * 1000);
+// BigDecimal tmpDays = new BigDecimal(days); //鐩稿樊澶╂暟
+// BigDecimal monthDay = null;
+// Calendar newFromMaxDay = Calendar.getInstance();
+// newFromMaxDay.set(newFrom.get(Calendar.YEAR), newFrom.get(Calendar.MONTH), 1, 0, 0, 0);
+// newFromMaxDay.add(Calendar.MONTH, 1); //涓嬩釜鏈�1鍙�
+// //鍦ㄥ綋鍓嶆湀涓� 杩欏潡鏈夐棶棰�
+// if (toDate.getTime() < newFromMaxDay.getTime().getTime()) {
+// monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
+// return tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).doubleValue();
+// }
+// // 涓婃湀澶╂暟
+// days = (newFromMaxDay.getTimeInMillis() - t1) * 1.00 / (24 * 60 * 60 * 1000);
+// tmpDays = new BigDecimal(days);
+// monthDay = new BigDecimal(newFrom.getActualMaximum(Calendar.DAY_OF_MONTH));
+// BigDecimal preRresMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP);
+//
+// //涓嬫湀澶╂暟
+// days = (t2 - newFromMaxDay.getTimeInMillis()) * 1.00 / (24 * 60 * 60 * 1000);
+// tmpDays = new BigDecimal(days);
+// monthDay = new BigDecimal(newFromMaxDay.getActualMaximum(Calendar.DAY_OF_MONTH));
+// resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result)).add(preRresMonth).doubleValue();
+// return resMonth;
+// }
+
+ /**
+ * 閫氳繃鏃堕棿绉掓绉掓暟鍒ゆ柇涓や釜鏃堕棿鐨勯棿闅�
+ *
+ * @param date1
+ * @param date2
+ * @return
+ */
+ public static int differentDaysUp(Date date1, Date date2) {
+ double days = ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24 * 1.00));
+ return new Double(Math.ceil(days)).intValue();
+ }
+
+ /**
+ * 鑾峰彇涓や釜鏃ユ湡涔嬮棿鐨勬墍鏈夋湀浠� (骞存湀)
+ *
+ * @param startTime
+ * @param endTime
+ * @return锛歭ist
+ */
+ public static List<String> getMonthBetweenDate(Date startTime, Date endTime) {
+ return getMonthBetweenDate(DateUtil.getFormatTimeStringA(startTime), DateUtil.getFormatTimeStringA(endTime));
+ }
+
+ /**
+ * 鑾峰彇涓や釜鏃ユ湡涔嬮棿鐨勬墍鏈夋湀浠� (骞存湀)
+ *
+ * @param startTime
+ * @param endTime
+ * @return锛歭ist
+ */
+ public static List<String> getMonthBetweenDate(String startTime, String endTime) {
+ SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMATE_STRING_Q);
+ // 澹版槑淇濆瓨鏃ユ湡闆嗗悎
+ List<String> list = new ArrayList<>();
+ try {
+ // 杞寲鎴愭棩鏈熺被鍨�
+ Date startDate = sdf.parse(startTime);
+ Date endDate = sdf.parse(endTime);
+
+ //鐢–alendar 杩涜鏃ユ湡姣旇緝鍒ゆ柇
+ Calendar calendar = Calendar.getInstance();
+ while (startDate.getTime() <= endDate.getTime()) {
+
+ // 鎶婃棩鏈熸坊鍔犲埌闆嗗悎
+ list.add(sdf.format(startDate));
+
+ // 璁剧疆鏃ユ湡
+ calendar.setTime(startDate);
+
+ //鎶婃湀鏁板鍔� 1
+ calendar.add(Calendar.MONTH, 1);
+
+ // 鑾峰彇澧炲姞鍚庣殑鏃ユ湡
+ startDate = calendar.getTime();
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return list;
+ }
+
+ /**
+ * 闄ゅ幓 灏忔椂 鍒� 绉�
+ *
+ * @param time
+ * @return
+ */
+ public static Date timeToDate(Date time) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(time);
+ setTimeToMidnight(calendar);
+ return calendar.getTime();
+ }
+
+ /**
+ * 闄ゅ幓 灏忔椂 鍒� 绉�
+ *
+ * deadtime 鏈潵灏卞皯浜嗕竴绉�
+ *
+ * @param time
+ * @return
+ */
+ public static Date deadTimeToDate(Date time) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(time);
+ calendar.add(Calendar.SECOND,1);
+ setTimeToMidnight(calendar);
+ return calendar.getTime();
+ }
+
+ public static boolean sameMonthDay(Date startDate, Date endDate) {
+ Calendar startCalendar = Calendar.getInstance();
+ startCalendar.setTime(startDate);
+ Calendar endCalender = Calendar.getInstance();
+ endCalender.setTime(endDate);
+ if (
+ startCalendar.get(Calendar.DAY_OF_MONTH) == endCalender.get(Calendar.DAY_OF_MONTH)
+ && startCalendar.get(Calendar.HOUR_OF_DAY) == endCalender.get(Calendar.HOUR_OF_DAY)
+ ) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public static double dayCompare(Date fromDate, Date toDate) {
+ return dayCompare(fromDate, toDate, false);
+ }
+
+ /**
+ * 璁$畻 fromDate 2023-01-12 toDate 2023-09-15
+ * 2023-01-12--->2023-01-01 ---> 2023-09-01 ------> 2023-09-15
+ * fromDate ---> fromDateFirstDate ---> toDateFirstDate ----> toDate
+ *
+ * @param fromDate
+ * @param toDate
+ * @return
+ */
+ public static double dayCompare(Date fromDate, Date toDate, boolean plusOneSec) {
+
+ if (plusOneSec) {
+ Calendar toD = Calendar.getInstance();
+ toD.setTime(toDate);
+ toD.add(Calendar.SECOND, 1);
+ toDate = toD.getTime();
+ }
+
+
+ //todo 闇�瑕佽绠椾笁绔椂闂� 鐩稿姞鍗冲彲
+ Date fromDateFirstDate = fromDate; // 绗竴涓�1鏃�
+
+ Date toDateFirstDate = toDate; // 鏈�鍚庝竴涓�1鏃�
+
+ boolean firstDay = true;
+
+ //todo 1.0 璁$畻 fromDateFirstDate
+ Calendar fromDateCal = Calendar.getInstance();
+ fromDateCal.setTime(fromDate);
+ fromDateCal.set(Calendar.DAY_OF_MONTH, 1);
+ fromDateCal.set(Calendar.HOUR_OF_DAY, 0);
+ fromDateCal.set(Calendar.MINUTE, 0);
+ if (fromDate.getTime() > fromDateCal.getTime().getTime()) {
+ fromDateCal.add(Calendar.MONTH, 1);
+ firstDay = false;
+ fromDateFirstDate = fromDateCal.getTime();
+ }
+
+ //todo 2.0 璁$畻 toDateFirstDate
+ Calendar toDateCal = Calendar.getInstance();
+ toDateCal.setTime(toDate);
+ toDateCal.set(Calendar.DAY_OF_MONTH, 1);
+ toDateCal.set(Calendar.HOUR_OF_DAY, 0);
+ toDateCal.set(Calendar.MINUTE, 0);
+
+ if (toDate.getTime() > toDateCal.getTime().getTime()) {
+ toDateFirstDate = toDateCal.getTime();
+ }
+
+ // todo 3.0 璁$畻鏁存暟鏈� fromDateFirstDate ---> toDateFirstDate
+ Calendar from = Calendar.getInstance();
+ from.setTime(fromDateFirstDate);
+ Calendar to = Calendar.getInstance();
+ to.setTime(toDateFirstDate);
+ //姣旇緝鏈堜唤宸� 鍙兘鏈夋暣鏁� 涔熶細璐熸暟
+ int result = to.get(Calendar.MONTH) - from.get(Calendar.MONTH);
+ //姣旇緝骞村樊
+ int month = (to.get(Calendar.YEAR) - from.get(Calendar.YEAR)) * 12;
+ //鐪熷疄 鐩稿樊鏈堜唤
+ result = result + month;
+
+ //todo 3.1 濡傛灉 fromDate 鍜宼oDate 鏄悓涓�澶� 鍒欑洿鎺ヨ繑鍥炴暣鏈堬紝涓嶅啀璁$畻 4.0 鍜�5.0
+ if (DateUtil.sameMonthDay(fromDate, toDate)) {
+ return firstDay ? result : result + 1;
+ }
+
+ // todo 4.0 璁$畻 fromDate ---> fromDateFirstDate 鐨勬湀浠�
+ double days = (fromDateFirstDate.getTime() - fromDate.getTime()) * 1.00 / (24 * 60 * 60 * 1000);
+ BigDecimal tmpDays = new BigDecimal(days); //鐩稿樊澶╂暟
+ BigDecimal monthDay = new BigDecimal(DateUtil.getMonthDay(fromDate));
+ BigDecimal resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(new BigDecimal(result));
+
+ // todo 5.0 璁$畻 toDateFirstDate ----> toDate 鏈堜唤
+ days = (toDate.getTime() - toDateFirstDate.getTime()) * 1.00 / (24 * 60 * 60 * 1000);
+ tmpDays = new BigDecimal(days); //鐩稿樊澶╂暟
+ monthDay = new BigDecimal(DateUtil.getMonthDay(toDate));
+ resMonth = tmpDays.divide(monthDay, 4, BigDecimal.ROUND_HALF_UP).add(resMonth);
+
+ return resMonth.doubleValue();
+
+ }
+
+ public static Date getTargetEndTime(double month, Date startDate) {
+ Calendar endDate = Calendar.getInstance();
+ endDate.setTime(startDate);
+
+ Double intMonth = Math.floor(month);
+ endDate.add(Calendar.MONTH, intMonth.intValue());
+ double doubleMonth = month - intMonth;
+ if (doubleMonth <= 0) {
+ return endDate.getTime();
+ }
+ int futureDay = endDate.getActualMaximum(Calendar.DAY_OF_MONTH);
+ Double hour = doubleMonth * futureDay * 24;
+ endDate.add(Calendar.HOUR_OF_DAY, hour.intValue());
+ return endDate.getTime();
+ }
+
+ public static String getNextSecTime(String time) {
+ Date tTime = getDateFromStringA(time);
+ return getNextSecTime(tTime);
+ }
+
+ public static Date getNextSecDateTime(Date time) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(time);
+ calendar.add(Calendar.SECOND, 1);
+ return calendar.getTime();
+ }
+
+ public static String getNextSecTime(Date time) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(time);
+ calendar.add(Calendar.SECOND, 1);
+ return getFormatTimeStringA(calendar.getTime());
+ }
+
+ public static String getPreSecTime(String time) {
+ Date tTime = getDateFromStringA(time);
+ return getPreSecTime(tTime);
+ }
+
+ public static String getPreSecTime(Date time) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(time);
+ calendar.add(Calendar.SECOND, -1);
+ return getFormatTimeStringA(calendar.getTime());
+ }
+ public static Date getPreSecTime(Date time,int month) {
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(time);
+ calendar.add(Calendar.MONTH,month);
+ calendar.add(Calendar.SECOND, -1);
+ return calendar.getTime();
+ }
+ public static String getPreSecTimeStr(Date time,int month) {
+ return getFormatTimeStringB(getPreSecTime(time,month));
+ }
+ public static String calculateTimeDifference(Date start, Date end) {
+ Duration duration = Duration.between(start.toInstant(), end.toInstant());
+
+ long days = duration.toDays();
+ long hours = duration.toHours() % 24;
+ long minutes = duration.toMinutes() % 60;
+
+ StringBuilder result = new StringBuilder();
+
+ if (days > 0) {
+ result.append(days).append("澶�");
+ }
+
+ if (hours > 0) {
+ result.append(hours).append("鏃�");
+ }
+
+ if (minutes > 0 || (days == 0 && hours == 0 && minutes == 0)) {
+ result.append(minutes).append("鍒�");
+ }
+
+ return result.toString();
+ }
+
+ /**
+ * 璁$畻涓や釜瀛楃涓叉棩鏈熶箣闂寸殑鏈堟暟锛堣秴杩囦竴涓暣鏈堝垯+1锛�
+ * @param startDateStr 寮�濮嬫棩鏈熷瓧绗︿覆
+ * @param endDateStr 缁撴潫鏃ユ湡瀛楃涓�
+ * @return 鏈堟暟宸�
+ */
+ public static int calculateMonths(String startDateStr, String endDateStr) {
+ startDateStr = dateTimeToDate(startDateStr);
+ endDateStr = dateTimeToDate(endDateStr);
+ // 瑙f瀽鏃堕棿瀛楃涓蹭负LocalDateTime
+ LocalDateTime start = LocalDateTime.parse(startDateStr + " 00:00:00", DEFAULT_FORMATTER);
+ LocalDateTime end = LocalDateTime.parse(endDateStr + " 23:59:59", DEFAULT_FORMATTER);
+
+ // 纭繚寮�濮嬫椂闂村湪缁撴潫鏃堕棿涔嬪墠
+ if (start.isAfter(end)) {
+ LocalDateTime temp = start;
+ start = end;
+ end = temp;
+ }
+
+ // 鎻愬彇鏃ユ湡閮ㄥ垎璁$畻瀹屾暣鏈堟暟
+ Period period = Period.between(start.toLocalDate(), end.toLocalDate());
+ int fullMonths = period.getYears() * 12 + period.getMonths();
+
+ // 璁$畻瀹屾暣鏈堟暟鍚庣殑鐩爣鏃堕棿锛堢敤浜庡垽鏂槸鍚﹀垰濂芥暣鏈堬級
+ LocalDateTime targetTime = start.plusYears(period.getYears()).plusMonths(period.getMonths());
+
+ // 鍒ゆ柇瀹為檯缁撴潫鏃堕棿鏄惁瓒呰繃鐩爣鏃堕棿锛堝嵆瀛樺湪棰濆鏃堕棿锛�
+ boolean hasExtraTime = end.isAfter(targetTime);
+
+ // 鏈夐澶栨椂闂村垯鍔�1涓湀
+ if (hasExtraTime) {
+ fullMonths++;
+ }
+ return fullMonths;
+ }
+
+ public static void main(String[] args) throws ParseException {
+
+// SimpleDateFormat sf = new SimpleDateFormat(DateUtil.DATE_FORMATE_STRING_A);
+// Calendar c = Calendar.getInstance();
+// c.setTime(DateUtil.getDateFromString("2021-12-03",DateUtil.DATE_FORMATE_STRING_A));
+// c.add(Calendar.DAY_OF_MONTH, 125);
+// System.out.println("澧炲姞涓�澶╁悗鏃ユ湡:"+sf.format(c.getTime()));
+ System.out.println(calculateMonths("2025-02-01 00:00:00", "2025-02-28"));
+ }
+
}
--
Gitblit v1.8.0