java 中怎么根据当前时间得到上周一和上周五的日期
/**
* 获取上周五时间
*/
public Date lastFirday() {
//作用防止周日得到本周日期
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 7 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 9);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 6);//这是从上周日开始数的到本周五为6
}
/**
* 获取上周一时间
*/
public Date lastMonday() {
Calendar calendar = Calendar.getInstance();
while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
calendar.add(Calendar.DAY_OF_WEEK, -1);
}
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int offset = 1 - dayOfWeek;
calendar.add(Calendar.DATE, offset - 7);
return DateTimeUtil.getFirstDayOfWeek(calendar.getTime(), 2);
}
/**
* 得到某一天的该星期的第一日 00:00:00
*
* @param date
* @param firstDayOfWeek
* 一个星期的第一天为星期几
*
* @return
*/
public static Date getFirstDayOfWeek(Date date, int firstDayOfWeek) {
Calendar cal = Calendar.getInstance();
if (date != null)
cal.setTime(date);
cal.setFirstDayOfWeek(firstDayOfWeek);//设置一星期的第一天是哪一天
cal.set(Calendar.DAY_OF_WEEK, firstDayOfWeek);//指示一个星期中的某天
cal.set(Calendar.HOUR_OF_DAY, 0);//指示一天中的小时。HOUR_OF_DAY
用于 24 小时制时钟。例如,在 10:04:15.250 PM 这一时刻,HOUR_OF_DAY
为 22。
cal.set(Calendar.MINUTE, 0);//指示一小时中的分钟。例如,在 10:04:15.250 PM 这一时刻,MINUTE
为 4。
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTime();
}