得到本周的日期

1、先写一个工具类 DateUtil 

/**
* 0-6,周日是0,周六是6
*/
public static int getWeekIndex(Date date){
Calendar cale = Calendar.getInstance();
cale.setTime(date);
int w = cale.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0){
w = 0;
}
return w;
}
/**
* 增加天数,可为负数
*/
public static Date addDays(Date date,int days){
if (date == null) return null;
Date ret = new Date();
ret.setTime(date.getTime() + days * 24 * 60 * 60 * 1000L);
return ret;
}

2、使用
int weekIndex = DateUtil.getWeekIndex(new Date());
Date date = new Date();
星期天日期:
DateUtil.addDays(date,-weekIndex)
星期六日期:
DateUtil.addDays(date,6-weekIndex)
posted @ 2021-11-17 16:03  举个栗子⊙  阅读(56)  评论(0)    收藏  举报