From 890d242d8b36aecd36c4a5f506384a08ea7b12a6 Mon Sep 17 00:00:00 2001
From: Your Name <you@example.com>
Date: 星期五, 08 九月 2023 20:27:51 +0800
Subject: [PATCH] 优化代码
---
springboot/src/test/java/com/java110/AppTest.java | 101 +++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 87 insertions(+), 14 deletions(-)
diff --git a/springboot/src/test/java/com/java110/AppTest.java b/springboot/src/test/java/com/java110/AppTest.java
index b85e6cf..537e3fa 100644
--- a/springboot/src/test/java/com/java110/AppTest.java
+++ b/springboot/src/test/java/com/java110/AppTest.java
@@ -11,6 +11,7 @@
import org.junit.Test;
import java.math.BigDecimal;
+import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@@ -19,25 +20,26 @@
/**
* Unit test for simple App.
*/
-public class AppTest
-{
+public class AppTest {
/**
* Rigorous Test :-)
*/
@Test
- public void should()
- {
+ public void should() {
//瑙e瘑
- FeeDto feeDto = new FeeDto();
- feeDto.setStartTime(DateUtil.getDateFromStringB("2023-01-01"));
- feeDto.setEndTime(DateUtil.getDateFromStringB("2023-01-12"));
- feeDto.setPrepaymentPeriod("20");
- feeDto.setState(FeeDto.STATE_DOING);
- feeDto.setConfigEndTime(DateUtil.getDateFromStringB("2050-01-01"));
- feeDto.setFeeFlag(FeeDto.FEE_FLAG_CYCLE);
- feeDto.setPaymentCd("1200");
- feeDto.setPaymentCycle("12");
- getTargetEndDateAndOweMonth(feeDto,null);
+// FeeDto feeDto = new FeeDto();
+// feeDto.setStartTime(DateUtil.getDateFromStringB("2023-01-01"));
+// feeDto.setEndTime(DateUtil.getDateFromStringB("2023-01-12"));
+// feeDto.setPrepaymentPeriod("20");
+// feeDto.setState(FeeDto.STATE_DOING);
+// feeDto.setConfigEndTime(DateUtil.getDateFromStringB("2050-01-01"));
+// feeDto.setFeeFlag(FeeDto.FEE_FLAG_CYCLE);
+// feeDto.setPaymentCd("1200");
+// feeDto.setPaymentCycle("12");
+// getTargetEndDateAndOweMonth(feeDto,null);
+// 0.3667 0.3226
+ double month = dayCompare(DateUtil.getDateFromStringB("2023-01-12"), DateUtil.getDateFromStringB("2023-09-15"));
+ System.out.println("month=" + month);
}
public Map getTargetEndDateAndOweMonth(FeeDto feeDto, OwnerCarDto ownerCarDto) {
@@ -188,7 +190,78 @@
return endDate.getTime();
}
+
+ /**
+ * 璁$畻 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 double dayCompare(Date fromDate, Date toDate) {
+
+
+ //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);
+ 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);
+ 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 double dayCompare1(Date fromDate, Date toDate) {
double resMonth = 0.0;
Calendar from = Calendar.getInstance();
from.setTime(fromDate);
--
Gitblit v1.8.0