Browse Source

add method

Looly 5 years ago
parent
commit
01ac41f08e

+ 1 - 0
CHANGELOG.md

@@ -8,6 +8,7 @@
 ### 新特性
 * 【socket】     对NioServer和NioClient改造(pr#992@Github)
 * 【core  】     StrUtil增加filter方法(pr#149@Gitee)
+* 【core  】     DateUtil增加beginOfWeek重载
 
 ### Bug修复#
 

+ 24 - 0
hutool-core/src/main/java/cn/hutool/core/date/DateUtil.java

@@ -1042,6 +1042,18 @@ public class DateUtil extends CalendarUtil {
 	}
 
 	/**
+	 * 获取某周的开始时间
+	 *
+	 * @param date               日期
+	 * @param isMondayAsFirstDay 是否周一做为一周的第一天(false表示周日做为第一天)
+	 * @return {@link DateTime}
+	 * @since 5.4.0
+	 */
+	public static DateTime beginOfWeek(Date date, boolean isMondayAsFirstDay) {
+		return new DateTime(beginOfWeek(calendar(date), isMondayAsFirstDay));
+	}
+
+	/**
 	 * 获取某周的结束时间,周日定为一周的结束
 	 *
 	 * @param date 日期
@@ -1052,6 +1064,18 @@ public class DateUtil extends CalendarUtil {
 	}
 
 	/**
+	 * 获取某周的结束时间
+	 *
+	 * @param date              日期
+	 * @param isSundayAsLastDay 是否周日做为一周的最后一天(false表示周六做为最后一天)
+	 * @return {@link DateTime}
+	 * @since 5.4.0
+	 */
+	public static DateTime endOfWeek(Date date, boolean isSundayAsLastDay) {
+		return new DateTime(endOfWeek(calendar(date), isSundayAsLastDay));
+	}
+
+	/**
 	 * 获取某月的开始时间
 	 *
 	 * @param date 日期

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

@@ -99,7 +99,7 @@ public class DateUtilTest {
 	}
 
 	@Test
-	public void beginAndWeedTest() {
+	public void beginOfWeekTest() {
 		String dateStr = "2017-03-01 22:33:23";
 		DateTime date = DateUtil.parse(dateStr);
 		Objects.requireNonNull(date).setFirstDayOfWeek(Week.MONDAY);
@@ -121,7 +121,7 @@ public class DateUtilTest {
 	}
 
 	@Test
-	public void beginAndWeedTest2() {
+	public void beginOfWeekTest2() {
 		String beginStr = "2020-03-11";
 		DateTime date = DateUtil.parseDate(beginStr);
 		Calendar calendar = date.toCalendar();