package cn.com.ctop.common.utils; import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class DateUtils { public static List> getDateDayRange(String startDate, String endDate) throws ParseException { List> list = new ArrayList>(); String splitDate = DateUtils.addDay(endDate, -1); splitDate = DateUtils.addDay(splitDate, 1); if (DateUtils.compareDate(startDate, splitDate) >= 0) { Map map = new HashMap(); map.put("startDate", startDate); map.put("endDate", endDate); list.add(map); } else { while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) { Map map = new HashMap(); map.put("startDate", splitDate); map.put("endDate", endDate); list.add(map); splitDate = DateUtils.addDay(splitDate, -1); endDate = DateUtils.addDay(endDate, -1); if (DateUtils.compareDate(startDate, splitDate) > 0) { splitDate = startDate; } } } return list; } public static List> getDateWeekRange(String startDate, String endDate) throws ParseException { List> list = new ArrayList>(); String splitDate = DateUtils.addWeek(endDate, -1); splitDate = DateUtils.addDay(splitDate, 1); if (DateUtils.compareDate(startDate, splitDate) >= 0) { Map map = new HashMap(); map.put("startDate", startDate); map.put("endDate", endDate); list.add(map); } else { while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) { Map map = new HashMap(); map.put("startDate", splitDate); map.put("endDate", endDate); list.add(map); splitDate = DateUtils.addWeek(splitDate, -1); endDate = DateUtils.addWeek(endDate, -1); if (DateUtils.compareDate(startDate, splitDate) > 0) { splitDate = startDate; } } } return list; } public static List> getDateMonthRange(String startDate, String endDate) throws ParseException { List> list = new ArrayList>(); String splitDate = DateUtils.addMonth(endDate, -1); splitDate = DateUtils.addDay(splitDate, 1); if (DateUtils.compareDate(startDate, splitDate) >= 0) { Map map = new HashMap(); map.put("startDate", startDate); map.put("endDate", endDate); list.add(map); } else { while (DateUtils.compareDate(startDate, splitDate) < 0 || DateUtils.compareDate(startDate, endDate) <= 0) { Map map = new HashMap(); map.put("startDate", splitDate); map.put("endDate", endDate); list.add(map); splitDate = DateUtils.addMonth(splitDate, -1); endDate = DateUtils.addMonth(endDate, -1); if (DateUtils.compareDate(startDate, splitDate) > 0) { splitDate = startDate; } } } return list; } public static String addMonth(String dateString, int month) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(dateString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MONTH, month); return sdf.format(calendar.getTime()); } public static String addWeek(String dateString, int week) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(dateString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.WEEK_OF_YEAR, week); return sdf.format(calendar.getTime()); } public static String addDay(String dateString, int day) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date date = sdf.parse(dateString); Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, day); return sdf.format(calendar.getTime()); } public static Date addDay(Date date, int day) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.DAY_OF_YEAR, day); return calendar.getTime(); } public static Date addSecond(Date date, int seconds) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.SECOND, seconds); return calendar.getTime(); } public static int compareDate(String firstDateString, String secondDateString) throws ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date firstDate = sdf.parse(firstDateString); Date secondDate = sdf.parse(secondDateString); long first = firstDate.getTime(); long second = secondDate.getTime(); return first == second ? 0 : (first > second ? 1 : -1); } public static String timeStamp2Date(Timestamp timeLong) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式 Date date; try { date = sdf.parse(sdf.format(timeLong)); return sdf.format(date); } catch (ParseException e) { e.printStackTrace(); return null; } } public static String timeStamp2Date(long currentTimeMillis) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式 Date date; try { date = sdf.parse(sdf.format(currentTimeMillis)); return sdf.format(date); } catch (ParseException e) { e.printStackTrace(); return null; } } }