希望他能对你有所帮助
public static Map<String, String> getWeekDate() {
Map<String, String> map = new HashMap<String, String>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayWeek == 1) {
dayWeek = 8;
}
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - dayWeek);// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
Date mondayDate = cal.getTime();
String weekBegin = sdf.format(mondayDate);
cal.add(Calendar.DATE, 4 + cal.getFirstDayOfWeek());
Date sundayDate = cal.getTime();
String weekEnd = sdf.format(sundayDate);
map.put("mondayDate", weekBegin);
map.put("sundayDate", weekEnd);
return map;
}
这个编码习惯不规范