Java获取指定时间段的年份(开始、结束时间)、月份(开始、结束时间)、天数(开始、结束时间)
package test; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; public class DateTest { public static void main(String[] args) { System.out.println(getYears("2016-01-01 00:00:00","2018-07-01 00:00:00")); System.out.println(getMonths("2018-01-01 00:00:00","2018-07-01 00:00:00")); System.out.println(getDays("2018-06-01 00:00:00","2018-07-01 00:00:00")); } /*** * 获取两个时间段的年份/年第一天/年最后一天 * @param startTime * @param endTime * @return */ public static List<Map> getYears(String startTime, String endTime) { List<Map> res=new ArrayList<Map>(); DateFormat dateFormat = new SimpleDateFormat("yyyy"); DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date start = dateFormat.parse(startTime); Date end = dateFormat.parse(endTime); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(start); Calendar tempEnd = Calendar.getInstance(); tempEnd.setTime(end); tempEnd.add(Calendar.YEAR,1);// 日期加1(包含结束) while (tempStart.before(tempEnd)) { String year=dateFormat.format(tempStart.getTime()); String first=year+"-01-01 00:00:00"; String last=year+"-12-31 23:59:59"; Map<String,Object> map=new HashMap<String,Object>(); map.put("year", year); map.put("first", dateFormat2.parse(first)); map.put("last", dateFormat2.parse(last)); res.add(map); tempStart.add(Calendar.YEAR,1); } } catch (ParseException e) { e.printStackTrace(); } return res; } /*** * 获取两个时间段的年份-月份/月第一天/月最后一天 * @param startTime * @param endTime * @return */ public static List<Map> getMonths(String startTime, String endTime) { List<Map> res=new ArrayList<Map>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM"); DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); DateFormat dateFormat3 = new SimpleDateFormat("yyyy-MM-dd"); try { Date start = dateFormat.parse(startTime); Date end = dateFormat.parse(endTime); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(start); Calendar tempEnd = Calendar.getInstance(); tempEnd.setTime(end); tempEnd.add(Calendar.MONTH,1);// 日期加1(包含结束) while (tempStart.before(tempEnd)) { String month=dateFormat.format(tempStart.getTime()); tempStart.set(Calendar.DAY_OF_MONTH, 1); String first=dateFormat3.format(tempStart.getTime()); tempStart.set(Calendar.DAY_OF_MONTH, tempStart.getActualMaximum(Calendar.DAY_OF_MONTH)); Map<String,Object> map=new HashMap<String,Object>(); map.put("month", month); map.put("first", dateFormat2.parse(first+" 00:00:00")); map.put("last", dateFormat2.parse(first+" 23:59:59")); res.add(map); tempStart.add(Calendar.MONTH,1); } } catch (ParseException e) { e.printStackTrace(); } return res; } /*** * 获取两个时间段的天数/开始时间/结束时间 * @param startTime * @param endTime * @return */ public static List<Map> getDays(String startTime, String endTime) { // 返回的日期集合 List<Map> res=new ArrayList<Map>(); DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date start = dateFormat.parse(startTime); Date end = dateFormat.parse(endTime); Calendar tempStart = Calendar.getInstance(); tempStart.setTime(start); Calendar tempEnd = Calendar.getInstance(); tempEnd.setTime(end); tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束) while (tempStart.before(tempEnd)) { String day=dateFormat.format(tempStart.getTime()); Map<String,Object> map=new HashMap<String,Object>(); map.put("day", day); map.put("first", dateFormat2.parse(day+" 00:00:00")); map.put("last", dateFormat2.parse(day+" 23:59:59")); res.add(map); tempStart.add(Calendar.DAY_OF_YEAR, 1); } } catch (ParseException e) { e.printStackTrace(); } return res; } }