本頭條號主要是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();
}
}
大部分系統都是有圖表統計的,有圖表統計的話基本都能使用到這個工具類。這也是我在多個項目中都用到,所以封裝了這個工具類。
原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/223394.html