packagetest;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.ArrayList;importjava.util.Calendar;importjava.util.Date;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importjava.util.TreeMap;public classDateTest {public static voidmain(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"));
}/***
* 获取两个时间段的年份/年第一天/年最后一天
*@paramstartTime
*@paramendTime
*@return
*/
public static ListgetYears(String startTime, String endTime) {
List res=new ArrayList();
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 map=new HashMap();
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();
}returnres;
}/***
* 获取两个时间段的年份-月份/月第一天/月最后一天
*@paramstartTime
*@paramendTime
*@return
*/
public static ListgetMonths(String startTime, String endTime) {
List res=new ArrayList();
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 map=new HashMap();
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();
}returnres;
}/***
* 获取两个时间段的天数/开始时间/结束时间
*@paramstartTime
*@paramendTime
*@return
*/
public static ListgetDays(String startTime, String endTime) {//返回的日期集合
List res=new ArrayList();
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 map=new HashMap();
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();
}returnres;
}
}