mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix 把日期为设置为当前周的第一天代码块移动到 CalendarUtil 中
This commit is contained in:
parent
7d4d87be82
commit
3b6cca7435
@ -353,12 +353,26 @@ public class CalendarUtil {
|
|||||||
*
|
*
|
||||||
* @param cal1 日期1
|
* @param cal1 日期1
|
||||||
* @param cal2 日期2
|
* @param cal2 日期2
|
||||||
|
* @param isMon 是否为周一。国内第一天为星期一,国外第一天为星期日
|
||||||
* @return 是否为同一周
|
* @return 是否为同一周
|
||||||
*/
|
*/
|
||||||
public static boolean isSameWeek(Calendar cal1, Calendar cal2) {
|
public static boolean isSameWeek(Calendar cal1, Calendar cal2, boolean isMon) {
|
||||||
if (cal1 == null || cal2 == null) {
|
if (cal1 == null || cal2 == null) {
|
||||||
throw new IllegalArgumentException("The date must not be null");
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
}
|
}
|
||||||
|
// 把所传日期设置为其当前周的第一天
|
||||||
|
// 比较设置后的两个日期是否是同一天:true 代表同一周
|
||||||
|
if (isMon) {
|
||||||
|
cal1.setFirstDayOfWeek(Calendar.MONDAY);
|
||||||
|
cal1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
||||||
|
cal2.setFirstDayOfWeek(Calendar.MONDAY);
|
||||||
|
cal2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
||||||
|
} else {
|
||||||
|
cal1.setFirstDayOfWeek(Calendar.SUNDAY);
|
||||||
|
cal1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
||||||
|
cal2.setFirstDayOfWeek(Calendar.SUNDAY);
|
||||||
|
cal2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
||||||
|
}
|
||||||
return isSameDay(cal1, cal2);
|
return isSameDay(cal1, cal2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1598,20 +1598,7 @@ public class DateUtil extends CalendarUtil {
|
|||||||
if (date1 == null || date2 == null) {
|
if (date1 == null || date2 == null) {
|
||||||
throw new IllegalArgumentException("The date must not be null");
|
throw new IllegalArgumentException("The date must not be null");
|
||||||
}
|
}
|
||||||
Calendar calendar1 = calendar(date1);
|
return CalendarUtil.isSameWeek(calendar(date1), calendar(date2), isMon);
|
||||||
Calendar calendar2 = calendar(date2);
|
|
||||||
if (isMon) {
|
|
||||||
calendar1.setFirstDayOfWeek(Calendar.MONDAY);
|
|
||||||
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
||||||
calendar2.setFirstDayOfWeek(Calendar.MONDAY);
|
|
||||||
calendar2.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
|
|
||||||
} else {
|
|
||||||
calendar1.setFirstDayOfWeek(Calendar.SUNDAY);
|
|
||||||
calendar1.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
|
||||||
calendar2.setFirstDayOfWeek(Calendar.SUNDAY);
|
|
||||||
calendar2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
|
|
||||||
}
|
|
||||||
return CalendarUtil.isSameWeek(calendar1, calendar2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user