行业新闻
报表统计怎么做(月报表的统计制作方法)
2022-02-16 14:56  浏览:4

本头条号主要是Java常用关键技术点,通用工具类的分享;以及springboot+springcloud+Mybatisplus+druid+mysql+redis+swagger+maven+docker等集成框架的技术分享;datax、kafka、flink等大数据处理框架的技术分享。文章会不断更新,欢迎码友关注点赞收藏转发!

关注多的话,后面会录制一些视频教程,图文和视频结合,比如:图书介绍网站系统、抢购系统、大数据中台系统等。技术才是程序猿的最爱,码友们冲啊

正文

在java开发中,几乎所有系统都有报表统计的功能,而报表统计的其中一个参数就是时间段,有环比同比时间段统计,有时间段统计,有周统计,有趋势图统计等,所以封装了该工具类,避免在每个统计方法中重复的计算这样那样的日期。

照旧先上工具类使用栗子:

 System.out.println("201909环比日期="+ReportDateUtil.getHbMonth("201909"));
 System.out.println("201909同比日期="+ReportDateUtil.getTbMonth("201909"));
 System.out.println("201901-201909相隔月数="+ReportDateUtil.getPeriodAmount("20190101", "20190901", ReportDateUtil.MONTH));
 // 用于趋势图中显示日期,当统计值为0时也是要显示的哦
 System.out.println("201901-201909之间的日期列表="+ReportDateUtil.getPeriodDateList("20190101", "20190901", ReportDateUtil.MONTH));
 ReportDateUtil.getMonthWeekGroupList("201909").forEach((k, v) -> {
     // 计算9月份共有多少周,每周的日期 
     System.out.println("201909第"+k+"周:"+v.toString());
 });
 System.out.println("201909中的自然周为="+ReportDateUtil.getMonthWeekList("201909"));
 

上面使用例子打印如下

 201909环比日期=201908
 201909同比日期=201809
 201901-201909相隔月数=8
 201901-201909之间的日期列表=[201901, 201902, 201903, 201904, 201905, 201906, 201907, 201908, 201909]
 201909第1周:[2019-09-01, 2019-09-02, 2019-09-03, 2019-09-04, 2019-09-05, 2019-09-06, 2019-09-07]
 201909第2周:[2019-09-08, 2019-09-09, 2019-09-10, 2019-09-11, 2019-09-12, 2019-09-13, 2019-09-14]
 201909第3周:[2019-09-15, 2019-09-16, 2019-09-17, 2019-09-18, 2019-09-19, 2019-09-20, 2019-09-21]
 201909第4周:[2019-09-22, 2019-09-23, 2019-09-24, 2019-09-25, 2019-09-26, 2019-09-27, 2019-09-28]
 201909第5周:[2019-09-29, 2019-09-30]
 201909中的自然周为=[201939, 201936, 201938, 201937, 201940]

下面给出完整的工具类,这是我项目中在报表统计时经常用到的,有需要的码友可以先收藏,说不定哪天就用到了。

 import java.time.DayOfWeek;
 import java.time.LocalDate;
 import java.time.Period;
 import java.time.YearMonth;
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.ChronoUnit;
 import java.time.temporal.TemporalAdjusters;
 import java.time.temporal.WeekFields;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 public class ReportDateUtil {
     private static final DateTimeFormatter DTF_YYYYMMDD = DateTimeFormatter.ofPattern("yyyyMMdd");
     private static final DateTimeFormatter DTF_YYYYMM = DateTimeFormatter.ofPattern("yyyyMM");
     private static final DateTimeFormatter DTF_YYYY = DateTimeFormatter.ofPattern("yyyy");
     private static final String PATTERN_WEEK = "yyyyw";
     public static final int YEAR = 1;
     public static final int MONTH = 2;
     public static final int DAY = 3;
     public static void main(String[] args) {
         System.out.println("201909环比日期="+ReportDateUtil.getHbMonth("201909"));
         System.out.println("201909同比日期="+ReportDateUtil.getTbMonth("201909"));
         System.out.println("201901-201909相隔月数="+ReportDateUtil.getPeriodAmount("20190101", "20190901", ReportDateUtil.MONTH));
         System.out.println("201901-201909之间的日期列表="+ReportDateUtil.getPeriodDateList("20190101", "20190901", ReportDateUtil.MONTH));
         ReportDateUtil.getMonthWeekGroupList("201909").forEach((k, v) -> {
             System.out.println("201909第"+k+"周:"+v.toString());
         });
         System.out.println("201909中的自然周为="+ReportDateUtil.getMonthWeekList("201909"));
     }
     
     public static String getFirstOfMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.firstDayOfMonth()));
     }
     
     public static String getLastOfMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.lastDayOfMonth()));
     }
     
     public static String getFirstOfYear(String currYear) {
         currYear = currYear + "0101";
         LocalDate d = LocalDate.parse(currYear, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.firstDayOfYear()));
     }
     
     public static String getLastOfYear(String currYear) {
         currYear = currYear + "0101";
         LocalDate d = LocalDate.parse(currYear, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.lastDayOfYear()));
     }
     
     public static String getHbMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMM.format(d.minusMonths(1L));
     }
     
     public static String getTbMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMM.format(d.minusYears(1L));
     }
     
     public static List<String> getPeriodDateList(String startDate, String endDate, int unit) {
         List<String> l = new ArrayList<>();
         LocalDate start = LocalDate.parse(startDate, DTF_YYYYMMDD);
         LocalDate end = LocalDate.parse(endDate, DTF_YYYYMMDD);
         Period p = Period.between(start, end);
         if (ReportDateUtil.YEAR == unit) {
             for (int i = 0; i <= p.getYears(); i++) {
                 l.add(DTF_YYYY.format(start.plusYears(i)));
             }
         } else if (ReportDateUtil.MONTH == unit) {
             for (int i = 0; i <= p.getMonths(); i++) {
                 l.add(DTF_YYYYMM.format(start.plusMonths(i)));
             }
         } else if (ReportDateUtil.DAY == unit) {
             for (int i = 0; i <= p.getDays(); i++) {
                 l.add(DTF_YYYYMMDD.format(start.plusDays(i)));
             }
         }
         return l;
     }
     
     public static List<String> getPeriodDateList(String startDate, String endDate, int unit, String pattern) {
         DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
         List<String> l = new ArrayList<>();
         LocalDate start = LocalDate.parse(startDate, DTF_YYYYMMDD);
         LocalDate end = LocalDate.parse(endDate, DTF_YYYYMMDD);
         Period p = Period.between(start, end);
         if (ReportDateUtil.YEAR == unit) {
             for (int i = 0; i <= p.getYears(); i++) {
                 l.add(dtf.format(start.plusYears(i)));
             }
         } else if (ReportDateUtil.MONTH == unit) {
             for (int i = 0; i <= p.getMonths(); i++) {
                 l.add(dtf.format(start.plusMonths(i)));
             }
         } else if (ReportDateUtil.DAY == unit) {
             for (int i = 0; i <= p.getDays(); i++) {
                 l.add(dtf.format(start.plusDays(i)));
             }
         }
         return l;
     }
     
     public static List<String> getMonthWeekList(String currMonth) {
         return getMonthWeekList(currMonth, PATTERN_WEEK);
     }
     
     public static List<String> getMonthWeekList(String currMonth, String pattern) {
         final DateTimeFormatter DTF_WEEK = DateTimeFormatter.ofPattern(pattern);
         YearMonth yearMonth = YearMonth.parse(currMonth, DTF_YYYYMM);
         LocalDate start = LocalDate.now().with(yearMonth).with(TemporalAdjusters.firstDayOfMonth());
         LocalDate end = LocalDate.now().with(yearMonth).with(TemporalAdjusters.lastDayOfMonth());
         if(end.isAfter(LocalDate.now())){
             end = LocalDate.now();
         }
         // 按周分组,设置一周的开始日期为 星期天
         Map<String, List<LocalDate>> collect = Stream.iterate(start, localDate -> localDate.plusDays(1L))
                 .limit(ChronoUnit.DAYS.between(start, end) + 1)
                 .collect(Collectors.groupingBy(localDate -> DTF_WEEK.format(localDate)));
         List<String> l = new ArrayList<>();
         collect.forEach((k, v) -> l.add(k));
         return l;
     }
     
     public static Map<Integer, List<LocalDate>> getMonthWeekGroupList(String currMonth) {
         YearMonth yearMonth = YearMonth.parse(currMonth, DTF_YYYYMM);
         LocalDate start = LocalDate.now().with(yearMonth).with(TemporalAdjusters.firstDayOfMonth());
         LocalDate end = LocalDate.now().with(yearMonth).with(TemporalAdjusters.lastDayOfMonth());
         // 按周分组,设置一周的开始日期为 星期天
         return Stream.iterate(start, localDate -> localDate.plusDays(1L))
                 .limit(ChronoUnit.DAYS.between(start, end) + 1)
                 .collect(Collectors.groupingBy(localDate ->
                                                        localDate.get(WeekFields.of(DayOfWeek.SUNDAY, 1).weekOfMonth())));
     }
     
     public static int getPeriodAmount(String startDate, String endDate, int unit) {
         LocalDate start = LocalDate.parse(startDate, DTF_YYYYMMDD);
         LocalDate end = LocalDate.parse(endDate, DTF_YYYYMMDD);
         Period p = Period.between(start, end);
         if (ReportDateUtil.YEAR == unit) {
             return p.getYears();
         } else if (ReportDateUtil.MONTH == unit) {
             return p.getMonths();
         }
         return p.getDays();
     }
 }
 

大部分系统都是有图表统计的,有图表统计的话基本都能使用到这个工具类。这也是我在多个项目中都用到,所以封装了这个工具类。