@RequestMapping(value = "/date", method = RequestMethod.GET) public String agentHouseClick(DataRangRequest dataRangRequest,Model model){ String[] days = dataRangService.computeTimeRange(dataRangRequest.getTimeRange()); model.addAttribute("days",days); return "xxx"; } public interface DataRangService { String[] computeTimeRange(Integer timeRange); } @Service public class DataRangServiceImpl implements DataRangService { @Override public String[] computeTimeRange(Integer timeRange){ Calendar startTime = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); Calendar endTime = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); if(timeRange==1){ //近一周 startTime.add(Calendar.DATE,-7); return computeDate(startTime,endTime); }else if(timeRange==2){ //近二周 startTime.add(Calendar.DATE,-14); return computeDate(startTime,endTime); }else if(timeRange==3){ //本月 startTime.set(Calendar.DAY_OF_MONTH,1); return computeDate(startTime,endTime); }else if(timeRange==4){ //上个月 startTime.add(Calendar.MONTH,-1); startTime.set(Calendar.DAY_OF_MONTH,1); endTime.set(Calendar.DAY_OF_MONTH,0); return computeDate(startTime,endTime); } return null; } private String[] computeDate(Calendar startTime,Calendar endTime){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String day = sdf.format(startTime.getTime()); List<String> days = new LinkedList<>(); days.add(day); do{ startTime.add(Calendar.DAY_OF_YEAR,1); days.add(sdf.format(startTime.getTime())); }while(!(startTime.get(Calendar.YEAR)==endTime.get(Calendar.YEAR)&&startTime.get(Calendar.MONTH) ==endTime.get(Calendar.MONTH)&&startTime.get(Calendar.DAY_OF_MONTH)==endTime.get(Calendar.DAY_OF_MONTH))); String[] dayArr = days.toArray(new String[0]); return dayArr; } }