时间处理工具
package cn.com.utils; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import org.springframework.expression.ParseException; /** * 获取某一时间段特定星期几的日期 * @author * */ public class WeekDayUtil { public static void main(String[] args) throws java.text.ParseException { getDates("2018-05-27", "2018-06-11","Saturday|Sunday"); } /** * 获取某一时间段特定星期几的日期 * @param dateFrom 开始时间 * @param dateEnd 结束时间 * @param weekDays 星期 * @return 返回时间数组 * @throws java.text.ParseException */ public static String[] getDates(String dateFrom, String dateEnd, String weekDays) throws java.text.ParseException { long time = 1l; long perDayMilSec = 24 * 60 * 60 * 1000; List<String> dateList = new ArrayList<String>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //需要查询的星期系数 String strWeekNumber = weekForNum(weekDays); try { dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec); while (true) { time = sdf.parse(dateFrom).getTime(); time = time + perDayMilSec; Date date = new Date(time); dateFrom = sdf.format(date); if (dateFrom.compareTo(dateEnd) <= 0) { //查询的某一时间的星期系数 Integer weekDay = dayForWeek(date); //判断当期日期的星期系数是否是需要查询的 if (strWeekNumber.indexOf(weekDay.toString())!=-1) { System.out.println(dateFrom); dateList.add(dateFrom); } } else { break; } } } catch (ParseException e1) { e1.printStackTrace(); } String[] dateArray = new String[dateList.size()]; dateList.toArray(dateArray); return dateArray; } //等到当期时间的周系数。Sunday:1,Monday:2,Tuesday:3,Wednesday:4,Thursday:5,Friday:6,Saturday:7 public static Integer dayForWeek(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_WEEK); } /** * 得到对应星期的系数 Sunday:1,Monday:2,Tuesday:3,Wednesday:4,Thursday:5,Friday:6,Saturday:7 * @param weekDays 星期格式 Monday|Tuesday */ public static String weekForNum(String weekDays){ //返回结果为组合的星期系数 String weekNumber = ""; //解析传入的星期 if(weekDays.indexOf("|")!=-1){//多个星期数 String []strWeeks = weekDays.split("\\|"); for(int i=0;i<strWeeks.length;i++){ weekNumber = weekNumber + "" + getWeekNum(strWeeks[i]).toString(); } }else{//一个星期数 weekNumber = getWeekNum(weekDays).toString(); } return weekNumber; } //将星期转换为对应的系数 Sunday:1,Monday:2,Tuesday:3,Wednesday:4,Thursday:5,Friday:6,Saturday:7 public static Integer getWeekNum(String strWeek){ Integer number = 1;//默认为Sunday if("Sunday".equals(strWeek)){ number = 1; }else if("Monday".equals(strWeek)){ number = 2; }else if("Tuesday".equals(strWeek)){ number = 3; }else if("Wednesday".equals(strWeek)){ number = 4; }else if("Thursday".equals(strWeek)){ number = 5; }else if("Friday".equals(strWeek)){ number = 6; }else if("Saturday".equals(strWeek)){ number = 7; } return number; } }
package cn.com.utils; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; /** * 获取时间段内所有日期 * */ public class FindDates { public static void main(String[] args) throws Exception { Calendar cal = Calendar.getInstance(); String start = "2018-05-25"; String end = "2018-06-30"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date dBegin = sdf.parse(start); Date dEnd = sdf.parse(end); List<Date> lDate = findDates(dBegin, dEnd); for (Date date : lDate) { System.out.println(sdf.format(date)); } } public static List<Date> findDates(Date dBegin, Date dEnd) { List lDate = new ArrayList(); lDate.add(dBegin); Calendar calBegin = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calBegin.setTime(dBegin); Calendar calEnd = Calendar.getInstance(); // 使用给定的 Date 设置此 Calendar 的时间 calEnd.setTime(dEnd); // 测试此日期是否在指定日期之后 while (dEnd.after(calBegin.getTime())) { // 根据日历的规则,为给定的日历字段添加或减去指定的时间量 calBegin.add(Calendar.DAY_OF_MONTH, 1); lDate.add(calBegin.getTime()); } return lDate; } }
package cn.com.utils; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.List; /** * 对List中的日期进行排序 * @author yf */ public class DateSort { public static void main(String[] args) { List dateList = new ArrayList(); System.err.println("----before sort:"); for (int i = 0; i < 5; i++) {// 随机生成五个日期 Date d = new Date(System.currentTimeMillis() + (int) (Math.random() * 1000000000)); dateList.add(d); System.out.println(formatDate(d));// 格式化了日期 // System.out.println(d);//未格式化日期 } ComparatorDate c = new ComparatorDate(); Collections.sort(dateList, c); System.err.println("----after sort:"); for (Object date : dateList) { System.out.println(formatDate(date));// 格式化了日期 // System.out.println(date);//未格式化日期 } } /** * 格式化日期,返回字符串形式,形如"yyyy-MM-dd" * * @param date * @return */ private static String formatDate(Object date) { SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); return dateFormat.format(date); } } class ComparatorDate implements Comparator { public int compare(Object obj1, Object obj2) { Date begin = (Date) obj1; Date end = (Date) obj2; if (begin.after(end)) { return 1; } else { return -1; } } }