【工具类】日期工具类
根据日期获得所在周的日期
/**
* 根据日期获得所在周的日期
*
* @param mdate
* @return
*/
public static List<Date> dateToWeek(Date mdate) {
int b = mdate.getDay();
Date fdate;
List<Date> list = new ArrayList<Date>();
Long fTime = mdate.getTime() - b * 24 * 3600000;
for (int a = 1; a <= 7; a++) {
fdate = new Date();
fdate.setTime(fTime + (a * 24 * 3600000));
list.add(a - 1, fdate);
}
return list;
}
根据具体年份周数获取日期范围
/**
* 根据具体年份周数获取日期范围
*
* @param year
* @param week
* @param targetNum
* @return
*/
public static String getWeekDays(int year, int week, int targetNum, String format) {
// 计算目标周数
if (week + targetNum > 52) {
year++;
week += targetNum - 52;
} else if (week + targetNum <= 0) {
year--;
week += targetNum + 52;
} else {
week += targetNum;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
Calendar cal = Calendar.getInstance();
// 设置每周的开始日期
cal.setFirstDayOfWeek(Calendar.MONDAY);
cal.set(Calendar.YEAR, year);
cal.set(Calendar.WEEK_OF_YEAR, week);
cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
String beginDate = sdf.format(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
String endDate = sdf.format(cal.getTime());
return beginDate + "~" + endDate;
}
参考:
https://www.cnblogs.com/mithrandirw/p/8746468.html
https://blog.csdn.net/weixin_40211013/article/details/107310350