1、首先先获取本月日期:本月一号到本月月底——之前文章中有如何获取
2、List<String> listAllDaysByMonth=getAllDaysWeekByDate(firstDate,endDate);
3、
private List<String> getAllDaysWeekByDate(Date firstDate, Date endDate) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List<String> lst=new ArrayList();
while (!firstDate.after(endDate)) {
lst.add(sdf.format(firstDate));
firstDate = getNext(firstDate);
}
return lst;
}
private static Date getNext(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, 1);
return calendar.getTime();
}
4、最后获取的listAllDaysByMonth集合即为本月一月的日期集合格式是2018-08-17形式
获取本周的集合 大同小异,以前文章中没有写过获取本周的时间,这里主要把如何获取本周时间方法写出
Map<String, String> week = week();
String first = week.get("first");
String end = week.get("end");
private Map<String,String> week()throws Exception{
SimpleDateFormat fromat=new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
int d = 0;
if(cal.get(Calendar.DAY_OF_WEEK)==1){
d = -6;
}else{
d = 2-cal.get(Calendar.DAY_OF_WEEK);
}
cal.add(Calendar.DAY_OF_WEEK, d);
//所在周开始日期
String first=fromat.format(cal.getTime());
cal.add(Calendar.DAY_OF_WEEK, 6);
//所在周结束日期
String end=fromat.format(cal.getTime());
Map<String,String> map=new HashMap<String,String>();
map.put("first", first);
map.put("end", end);
return map;
}