报表统计怎么做(月报表的统计制作方法)


本头条号主要是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;
 /**
  * 报表日期工具类
  *
  * @author liang - liangxn
  * @date 2019/9/19 10:17
  */
 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"));
     }
     /**
      * 获取月的第一天
      *
      * @param currMonth 当前日期字符串,格式yyyyMM
      * @return
      */
     public static String getFirstOfMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.firstDayOfMonth()));
     }
     /**
      * 获取月的最后一天
      *
      * @param currMonth 当前日期字符串,格式yyyyMM
      * @return
      */
     public static String getLastOfMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.lastDayOfMonth()));
     }
     /**
      * 获取年的第一天
      *
      * @param currYear 当前日期字符串,格式yyyy
      * @return
      */
     public static String getFirstOfYear(String currYear) {
         currYear = currYear + "0101";
         LocalDate d = LocalDate.parse(currYear, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.firstDayOfYear()));
     }
     /**
      * 获取年的最后一天
      *
      * @param currYear 当前日期字符串,格式yyyy
      * @return
      */
     public static String getLastOfYear(String currYear) {
         currYear = currYear + "0101";
         LocalDate d = LocalDate.parse(currYear, DTF_YYYYMMDD);
         return DTF_YYYYMMDD.format(d.with(TemporalAdjusters.lastDayOfYear()));
     }
     /**
      * 计算环比月
      *
      * @param currMonth 当前日期字符串,格式yyyyMM
      * @return
      */
     public static String getHbMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMM.format(d.minusMonths(1L));
     }
     /**
      * 计算同比月
      *
      * @param currMonth 当前日期字符串,格式yyyyMM
      * @return
      */
     public static String getTbMonth(String currMonth) {
         currMonth = currMonth + "01";
         LocalDate d = LocalDate.parse(currMonth, DTF_YYYYMMDD);
         return DTF_YYYYMM.format(d.minusYears(1L));
     }
     /**
      * 计算两个日期之间的年(或月或日)的集合
      *
      * @param startDate 开始的日期 yyyyMMdd
      * @param endDate 结束的日期 yyyyMMdd
      * @param unit 年(或月或日)的标识,默认日
      * @return
      */
     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;
     }
     /**
      * 计算两个日期之间的年(或月或日)的集合
      *
      * @param startDate 开始的日期 yyyyMMdd
      * @param endDate 结束的日期 yyyyMMdd
      * @param unit 年(或月或日)的标识,默认日
      * @return
      */
     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;
     }
     /**
      * 计算某个月的的每一周在一年中属于第几周的集合,如2019年9月有6周(周一为一周开始),则返回结果为:[201934,201935,201936,201937,201938,201939]
      *
      * @param currMonth 年月字符串 yyyyMM
      * @return
      */
     public static List<String> getMonthWeekList(String currMonth) {
         return getMonthWeekList(currMonth, PATTERN_WEEK);
     }
     /**
      * 计算某个月的的每一周在一年中属于第几周的集合,如2019年9月有6周(周一为一周开始),则返回结果为:[201934,201935,201936,201937,201938,201939]
      *
      * @param currMonth 年月字符串 yyyyMM
      * @return
      */
     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;
     }
     /**
      * 计算某个月的自然周数的集合,按周分组,并列出每周的天日期,例如201909月有5周,每周的日期如下:
      * 第1周:[2019-09-01, 2019-09-02, 2019-09-03, 2019-09-04, 2019-09-05, 2019-09-06, 2019-09-07]
      * 第2周:[2019-09-08, 2019-09-09, 2019-09-10, 2019-09-11, 2019-09-12, 2019-09-13, 2019-09-14]
      * 第3周:[2019-09-15, 2019-09-16, 2019-09-17, 2019-09-18, 2019-09-19, 2019-09-20, 2019-09-21]
      * 第4周:[2019-09-22, 2019-09-23, 2019-09-24, 2019-09-25, 2019-09-26, 2019-09-27, 2019-09-28]
      * 第5周:[2019-09-29, 2019-09-30]
      *
      * @param currMonth 年月字符串 yyyyMM
      * @return
      */
     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())));
     }
     /**
      * 计算两个日期相隔多少年(或月或日)
      *
      * @param startDate 开始的日期 yyyyMMdd
      * @param endDate 结束的日期 yyyyMMdd
      * @param unit 计算年(或月或日)的标识,默认日
      * @return
      */
     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();
     }
 }
 

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发表评论

登录后才能评论