mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix date week bug
This commit is contained in:
parent
fad36a0833
commit
696a66f8db
@ -10,6 +10,7 @@
|
||||
### Bug修复
|
||||
* 【http 】 修复body方法添加多余头的问题(issue#769@Github)
|
||||
* 【bloomFilter 】修复默认为int类型,左移超过32位后,高位丢失问题(pr#770@Github)
|
||||
* 【core 】 修复beginOfWeek和endOfWeek一周开始计算错误问题(issue#I1BDPW@Gitee)
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -1169,9 +1169,7 @@ public class DateUtil {
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static Calendar beginOfWeek(Calendar calendar, boolean isMondayAsFirstDay) {
|
||||
if (isMondayAsFirstDay) {
|
||||
calendar.setFirstDayOfWeek(Calendar.MONDAY);
|
||||
}
|
||||
calendar.setFirstDayOfWeek(isMondayAsFirstDay ? Calendar.MONDAY : Calendar.SUNDAY);
|
||||
// WEEK_OF_MONTH为上限的字段(不包括),实际调整的为DAY_OF_MONTH
|
||||
return truncate(calendar, DateField.WEEK_OF_MONTH);
|
||||
}
|
||||
@ -1195,9 +1193,7 @@ public class DateUtil {
|
||||
* @since 3.1.2
|
||||
*/
|
||||
public static Calendar endOfWeek(Calendar calendar, boolean isSundayAsLastDay) {
|
||||
if (isSundayAsLastDay) {
|
||||
calendar.setFirstDayOfWeek(Calendar.MONDAY);
|
||||
}
|
||||
calendar.setFirstDayOfWeek(isSundayAsLastDay ? Calendar.MONDAY : Calendar.SUNDAY);
|
||||
// WEEK_OF_MONTH为上限的字段(不包括),实际调整的为DAY_OF_MONTH
|
||||
return ceiling(calendar, DateField.WEEK_OF_MONTH);
|
||||
}
|
||||
|
@ -114,6 +114,19 @@ public class DateUtilTest {
|
||||
Assert.assertEquals("2017-03-05 23:59:59", DateUtil.date(end).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void beginAndWeedTest2() {
|
||||
String beginStr = "2020-03-11";
|
||||
DateTime date = DateUtil.parseDate(beginStr);
|
||||
Calendar calendar = date.toCalendar();
|
||||
final Calendar begin = DateUtil.beginOfWeek(calendar, false);
|
||||
Assert.assertEquals("2020-03-08 00:00:00", DateUtil.date(begin).toString());
|
||||
|
||||
Calendar calendar2 = date.toCalendar();
|
||||
final Calendar end = DateUtil.endOfWeek(calendar2, false);
|
||||
Assert.assertEquals("2020-03-14 23:59:59", DateUtil.date(end).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void offsetDateTest() {
|
||||
String dateStr = "2017-03-01 22:33:23";
|
||||
|
Loading…
x
Reference in New Issue
Block a user