/** * 根据传入的日期集合 整理成时间段 */ public static String getDayToTimeSlot(List<String> list){ String time=""; Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { if(compareDate(o1,o2)){ return 1; } return -1; } }); String time1=""; String time2=""; for(int i=0;i<list.size();i++){ if(i==0){ time1=list.get(i); time2=list.get(i); } if(i+1<list.size()){ if(getAfterDayDate(list.get(i),"1").equals(list.get(i+1))){ time2=list.get(i+1); }else{ time+=time1+"至"+time2+","; time1=list.get(i+1); time2=list.get(i+1); } }else{ if(i==0){ time+=time1+"至"+time2+","; }else{ if(getAfterDayDate(list.get(i-1),"1").equals(list.get(i))){ time+=time1+"至"+time2+","; }else{ time+=time2+"至"+time2+","; } } } } return time; }
/** * 得到当前时间n天之后的日期 * * @param days * @return */ public static String getAfterDayDate(String time, String days) { int daysInt = Integer.parseInt(days); Calendar canlendar = Calendar.getInstance(); // java.util包 Date date = fomatDate(time); canlendar.setTime(date); canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动 Date time2 = canlendar.getTime(); if(time.length()>=12){ SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateStr = sdfd.format(time2); return dateStr; }else{ SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd"); String dateStr = sdfd.format(time2); return dateStr; } }