package com.aquilaflycloud.util; import net.sf.json.JSONArray; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class DateTools { public static void main(String[] args) { List<DateBean> list=new ArrayList<DateBean>(); getWeekDayTime(list, 4); // getYearDayTime(list, 4); // getDayLast(new ArrayList<DateBean>(), 7); // getDayTime(list,30); // getYearTimeDuring(list,5); System.out.println(JSONArray.fromObject(list).toString()); } public static void setTimeParam(List<DateBean> dateBeans,int timeType,String startTime,String endTime){ switch (timeType) { case 0: // * 0 :近7天 getDayLast(dateBeans,7); break; case 1: // * 1 :近1天 getDayLast(dateBeans,1); break; case 2: // * 2 :日统计 getDayTime(dateBeans,30); break; case 3: // * 3 :周统计 getWeekDayTime(dateBeans, 13); break; case 4: // * 4 :月统计 getYearDayTime(dateBeans, 12); case 5: //时间段查询 DateBean bean=new DateBean(); bean.setStartTime(stringToData(startTime)); bean.setEndTime(stringToData(endTime)); bean.setStartTimeTest(0); bean.setEndTimeTest(5); dateBeans.add(bean); break; case 6: //年统计 getYearTimeDuring(dateBeans,5); case 7: //最近30天 getDayLast(dateBeans,30); break; } } public static long stringToData(String stringDate){ String res; SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = null; try { // date = simpleDateFormat.parse("2017-1-12"+" 00:00:00"); date = simpleDateFormat.parse(stringDate+" 00:00:00"); } catch (ParseException e) { e.printStackTrace(); return 0; } return date.getTime()/1000; } //获取周时间戳段 (星期1:00:00:00 星期天:23:59:59) public static void getWeekDayTime(List<DateBean> timeList, int count) { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); Calendar calendar = Calendar.getInstance(); int diff = 7; for (int n = 0; n < count; n++) { DateBean bean = new DateBean(); calendar.setTime(new Date()); int qq= calendar.get(Calendar.DAY_OF_WEEK); if(qq==1){ qq=-6; }else{ qq=2-qq; } calendar.add(Calendar.DAY_OF_YEAR, qq- n * 7); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MINUTE, 0); bean.setStartTime(calendar.getTimeInMillis() / 1000); bean.setStartTimeTest(n * diff); // Date monday1 = calendar.getTime(); // System.out.println("周一:" + formatter.format(monday1) + ",时间戳:" + calendar.getTimeInMillis() / 1000); calendar.add(Calendar.DAY_OF_YEAR, 6); calendar.set(Calendar.HOUR_OF_DAY, 23); calendar.set(Calendar.SECOND, 59); calendar.set(Calendar.MINUTE, 59); bean.setEndTime(calendar.getTimeInMillis() / 1000); bean.setEndTimeTest(n * diff + diff); // monday1 = calendar.getTime(); // System.out.println("周日:" + formatter.format(monday1) + ",时间戳:" + calendar.getTimeInMillis() / 1000); timeList.add(bean); } } //获取月时间戳段 (1号:00:00:00 本月最后一号:23:59:59) public static void getYearDayTime(List<DateBean> timeList, int count) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); int diff = 30; for (int i = 0; i < count; i++) { DateBean bean = new DateBean(); //获取当前月第一天: Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -i); c.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天 c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MINUTE, 0); bean.setStartTime(c.getTimeInMillis() / 1000); bean.setStartTimeTest(i * diff); // String first = format.format(c.getTime()); // System.out.println("===============first:" + first); //获取当前月最后一天 c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH)); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.SECOND, 59); c.set(Calendar.MINUTE, 59); bean.setEndTime(c.getTimeInMillis() / 1000); bean.setEndTimeTest(i * diff + diff); // String last = format.format(c.getTime()); // System.out.println("===============last:" + last); timeList.add(bean); } } //获取最近天数的时间段 (不包括今天) public static void getDayLast(List<DateBean> timeList, int day) { SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); DateBean dateBean = new DateBean(); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_YEAR,-1); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.SECOND, 59); c.set(Calendar.MINUTE, 59); dateBean.setEndTime(c.getTimeInMillis() / 1000); dateBean.setEndTimeTest(day); c.add(Calendar.DAY_OF_YEAR, -day); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MINUTE, 0); dateBean.setStartTime(c.getTimeInMillis() / 1000); dateBean.setStartTimeTest(0); timeList.add(dateBean); } //获取按天统计时间戳(不包括今天) public static void getDayTime(List<DateBean> timeList, int count){ for (int i=0;i<count;i++){ DateBean dateBean = new DateBean(); Calendar c = Calendar.getInstance(); c.add(Calendar.DAY_OF_YEAR,-i-1); c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MINUTE, 0); dateBean.setStartTime(c.getTimeInMillis() / 1000); dateBean.setStartTimeTest(i+i); c.set(Calendar.HOUR_OF_DAY, 23); c.set(Calendar.SECOND, 59); c.set(Calendar.MINUTE, 59); dateBean.setEndTime(c.getTimeInMillis() / 1000); dateBean.setEndTimeTest(i+1+i); timeList.add(dateBean); } } public static void getYearTimeDuring(List<DateBean> timeList, int count){ Calendar calendar=Calendar.getInstance(); int year=calendar.get(Calendar.YEAR); for(int i=0;i<count;i++){ DateBean dateBean=new DateBean(); int y=year-i; calendar.set(y,0,1,0,0,0); dateBean.setStartTime(calendar.getTimeInMillis()/1000); calendar.set(y,11,31,23,59,59); dateBean.setEndTime(calendar.getTimeInMillis()/1000); timeList.add(dateBean); } } }
package com.aquilaflycloud.util; public class DateBean { long startTime; long endTime; int startTimeTest; int endTimeTest; public long getStartTime() { return startTime; } public void setStartTime(long startTime) { this.startTime = startTime; } public long getEndTime() { return endTime; } public void setEndTime(long endTime) { this.endTime = endTime; } public int getStartTimeTest() { return startTimeTest; } public void setStartTimeTest(int startTimeTest) { this.startTimeTest = startTimeTest; } public int getEndTimeTest() { return endTimeTest; } public void setEndTimeTest(int endTimeTest) { this.endTimeTest = endTimeTest; } }