ソースを参照

fix Date and aopbug

Looly 6 年 前
コミット
8bb73f3829

+ 2 - 0
CHANGELOG.md

@@ -19,6 +19,8 @@
 ### Bug修复
 * 【core   】     修复NumberWordFormatter拼写错误(issue#799@Github)
 * 【poi    】     修复xls文件下拉列表无效问题(issue#I1C79P@Gitee)
+* 【poi    】     修复使用Cglib代理问题(issue#I1C79P@Gitee)
+* 【core   】     修复DateUtil.weekCount跨年计算问题
 
 -------------------------------------------------------------------------------------------------------------
 ## 5.2.4

+ 3 - 2
hutool-aop/src/main/java/cn/hutool/aop/interceptor/CglibInterceptor.java

@@ -36,12 +36,13 @@ public class CglibInterceptor implements MethodInterceptor, Serializable {
 
 	@Override
 	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
+		final Object target = this.target;
 		Object result = null;
-
 		// 开始前回调
 		if (aspect.before(target, method, args)) {
 			try {
-				result = proxy.invokeSuper(obj, args);
+//				result = proxy.invokeSuper(obj, args);
+				result = proxy.invoke(target, args);
 			} catch (InvocationTargetException e) {
 				// 异常回调(只捕获业务代码导致的异常,而非反射导致的异常)
 				if (aspect.afterException(target, method, args, e.getTargetException())) {

+ 20 - 3
hutool-aop/src/test/java/cn/hutool/aop/test/AopTest.java

@@ -1,12 +1,12 @@
 package cn.hutool.aop.test;
 
+import cn.hutool.aop.ProxyUtil;
+import cn.hutool.aop.aspects.TimeIntervalAspect;
 import cn.hutool.core.lang.Console;
+import lombok.Data;
 import org.junit.Assert;
 import org.junit.Test;
 
-import cn.hutool.aop.ProxyUtil;
-import cn.hutool.aop.aspects.TimeIntervalAspect;
-
 /**
  * AOP模块单元测试
  *
@@ -27,6 +27,7 @@ public class AopTest {
 		Dog dog = ProxyUtil.proxy(new Dog(), TimeIntervalAspect.class);
 		String result = dog.eat();
 		Assert.assertEquals("狗吃肉", result);
+
 		dog.seize();
 	}
 
@@ -68,4 +69,20 @@ public class AopTest {
             Console.log("抓了只鸡");
 		}
 	}
+
+	@Test
+	public void testCGLIBProxy() {
+		TagObj target = new TagObj();
+		//目标类设置标记
+		target.setTag("tag");
+
+		TagObj proxy = ProxyUtil.proxy(target, TimeIntervalAspect.class);
+		//代理类获取标记tag (断言错误)
+		Assert.assertEquals("tag", proxy.getTag());
+	}
+
+	@Data
+	public static class TagObj{
+		private String tag;
+	}
 }

+ 19 - 15
hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java

@@ -1552,6 +1552,22 @@ public class DateUtil {
 	}
 
 	/**
+	 * 计算指定指定时间区间内的周数
+	 *
+	 * @param beginDate   开始时间
+	 * @param endDate     结束时间
+	 * @param isReset 是否重置时间为起始时间
+	 * @return 周数
+	 */
+	public static long betweenWeek(Date beginDate, Date endDate, boolean isReset) {
+		if (isReset) {
+			beginDate = beginOfDay(beginDate);
+			endDate = beginOfDay(endDate);
+		}
+		return between(beginDate, endDate, DateUnit.WEEK);
+	}
+
+	/**
 	 * 计算两个日期相差月数<br>
 	 * 在非重置情况下,如果起始日期的天小于结束日期的天,月数要少算1(不足1个月)
 	 *
@@ -1723,23 +1739,11 @@ public class DateUtil {
 	 * @param start 开始时间
 	 * @param end   结束时间
 	 * @return 周数
+	 * @deprecated 请使用 {@link #betweenWeek(Date, Date, boolean)}
 	 */
+	@Deprecated
 	public static int weekCount(Date start, Date end) {
-		final Calendar startCalendar = Calendar.getInstance();
-		startCalendar.setTime(start);
-		final Calendar endCalendar = Calendar.getInstance();
-		endCalendar.setTime(end);
-
-		final int startWeekofYear = startCalendar.get(Calendar.WEEK_OF_YEAR);
-		final int endWeekofYear = endCalendar.get(Calendar.WEEK_OF_YEAR);
-
-		int count = endWeekofYear - startWeekofYear + 1;
-
-		if (Calendar.SUNDAY != startCalendar.get(Calendar.DAY_OF_WEEK)) {
-			count--;
-		}
-
-		return count;
+		return (int) betweenWeek(start, end, true);
 	}
 
 	/**

+ 9 - 0
hutool-core/src/test/java/cn/hutool/core/date/DateUtilTest.java

@@ -719,4 +719,13 @@ public class DateUtilTest {
 		final LocalDateTime localDateTime = DateUtil.parseLocalDateTime(strDate, "yyyy-MM-dd");
 		Assert.assertEquals(strDate, DateUtil.format(localDateTime, DatePattern.NORM_DATE_PATTERN));
 	}
+
+	@Test
+	public void betweenWeekTest() {
+		final DateTime start = DateUtil.parse("2019-03-05");
+		final DateTime end = DateUtil.parse("2019-10-05");
+
+		final long weekCount = DateUtil.betweenWeek(start, end, true);
+		Assert.assertEquals(30L, weekCount);
+	}
 }