1.只需要传入type参数,即可自动判断得出开始结束时间:
type:今日:today、昨日:yesterday、本周:thisweek、上周:lastweek、本月:thismonth、
上月:lastmonth
private Map<String,String> getDateByType(String type){DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");//TODO 获取当前时间LocalDate inputDate = LocalDate.now();//当天String todayMin = LocalDateTime.of(inputDate, LocalTime.MIN).format(df);String todayMax = LocalDateTime.of(inputDate, LocalTime.MAX).format(df);//前一天String yestDayMin = LocalDateTime.of(inputDate, LocalTime.MIN).plusDays(-1).format(df);String yestDayMax = LocalDateTime.of(inputDate, LocalTime.MAX).plusDays(-1).format(df);//当周String weekMin = LocalDateTime.of(inputDate.with(DayOfWeek.MONDAY), LocalTime.MIN).format(df);String weekMax = LocalDateTime.of(inputDate.with(DayOfWeek.SUNDAY), LocalTime.MAX).format(df);//上周String weekMin1 = LocalDateTime.of(inputDate.with(DayOfWeek.MONDAY), LocalTime.MIN).plusWeeks(-1).format(df);String weekMax1 = LocalDateTime.of(inputDate.with(DayOfWeek.SUNDAY), LocalTime.MAX).plusWeeks(-1).format(df);//当月String monMin = LocalDateTime.of(inputDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN).format(df);String monMax = LocalDateTime.of(inputDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX).format(df);//上个月String monMin1 = LocalDateTime.of(inputDate.with(TemporalAdjusters.firstDayOfMonth()), LocalTime.MIN).plusMonths(-1).format(df);String monMax1 = LocalDateTime.of(inputDate.with(TemporalAdjusters.lastDayOfMonth()), LocalTime.MAX).plusMonths(-1).format(df);Map<String,String> dateMap = Maps.newHashMap();String begin="";String end="";switch (type){case "today":begin=todayMin;end=todayMax;break;case "yesterday":begin=yestDayMin;end=yestDayMax;break;case "thisweek":begin=weekMin;end=weekMax;break;case "lastweek":begin=weekMin1;end=weekMax1;break;case "thismonth":begin=monMin;end=monMax;break;case "lastmonth":begin=monMin1;end=monMax1;break;default :begin="1970-01-01";end=todayMax;}dateMap.put("begin",begin);dateMap.put("end",end);return dateMap;}