获取当周的周一
public static String getFirstDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
// 一周第一天为周日,所以这里+1
calendar.setWeekDate(calendar.getWeekYrae(), calendar.get(Calendar.WEEK_of_year), 2);
calendar.set(calendar.get(calendar.YEAR), calendar.get(Calendar.MONTH), calendar..get(calendar.DAY_OF_MONTH), 0, 0, 0);
return sdf.format(calendar.getTime());
}
获取当周的周日
public static String getLastDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
// 一周第一天为周日,所以这里下周一为第一天
calendar.setWeekDate(calendar.getWeekYrae(), calendar.get(Calendar.WEEK_of_year) + 1, 1);
calendar.set(calendar.get(calendar.YEAR), calendar.get(Calendar.MONTH), calendar..get(calendar.DAY_OF_MONTH), 23, 59, 59);
return sdf.format(calendar.getTime());
}
获取上周一的时间
public static String getLastMonday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
// 设置周一为第一天
calendar.setFirstDayOfWeek(Calendar.MONDAY);
// 日期减去7天
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
return sdf.format(calendar.getTime()) + " 00:00:00";
}
获取上周日的时间
public static String getLastSunday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar calendar = Calendar.getInstance();
// 设置周一为第一天
calendar.setFirstDayOfWeek(Calendar.MONDAY);
// 日期减去7天
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
return sdf.format(calendar.getTime()) + " 23:59:59";
}