|
@@ -13,6 +13,8 @@ import java.util.List;
|
|
|
|
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
import org.apache.commons.lang3.time.DateFormatUtils;
|
|
|
|
|
|
|
|
+import static com.slibra.common.constant.MyConstants.DEFAULT_DAYS_QUERY_COUNT;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 时间工具类
|
|
* 时间工具类
|
|
*
|
|
*
|
|
@@ -251,7 +253,12 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
return dates;
|
|
return dates;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取n个月之前的YYYY_MM格式的月份数据,输入参数也为YYYY_MM
|
|
|
|
+ * @param inputDate
|
|
|
|
+ * @param n
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
public static String getMonthsBefore(String inputDate, int n) {
|
|
public static String getMonthsBefore(String inputDate, int n) {
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
|
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM_DD);
|
|
DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern(YYYY_MM);
|
|
DateTimeFormatter formatterOut = DateTimeFormatter.ofPattern(YYYY_MM);
|
|
@@ -278,7 +285,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
|
|
|
|
|
|
|
|
public static String getNYearSameMonth(String inputDate, int n) {
|
|
public static String getNYearSameMonth(String inputDate, int n) {
|
|
- DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
|
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM);
|
|
try {
|
|
try {
|
|
// 解析输入的字符串为YearMonth对象
|
|
// 解析输入的字符串为YearMonth对象
|
|
YearMonth currentYearMonth = YearMonth.parse(inputDate, formatter);
|
|
YearMonth currentYearMonth = YearMonth.parse(inputDate, formatter);
|
|
@@ -302,7 +309,7 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
* @throws ParseException 如果日期字符串格式不正确
|
|
* @throws ParseException 如果日期字符串格式不正确
|
|
*/
|
|
*/
|
|
public static List<String> getAllDatesInRangeStr(String startDateStr, String endDateStr) {
|
|
public static List<String> getAllDatesInRangeStr(String startDateStr, String endDateStr) {
|
|
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD);
|
|
Date startDate = null;
|
|
Date startDate = null;
|
|
Date endDate = null;
|
|
Date endDate = null;
|
|
try {
|
|
try {
|
|
@@ -345,31 +352,58 @@ public class DateUtils extends org.apache.commons.lang3.time.DateUtils
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
+ public static List<String> getMonthsBetween(String startDateStr, String endDateStr) {
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYY_MM);
|
|
|
|
+ YearMonth startDate = YearMonth.parse(startDateStr, formatter);
|
|
|
|
+ YearMonth endDate = YearMonth.parse(endDateStr, formatter);
|
|
|
|
+
|
|
|
|
+ List<String> monthsBetween = new ArrayList<>();
|
|
|
|
+ YearMonth currentMonth = startDate;
|
|
|
|
+
|
|
|
|
+ while (!currentMonth.isAfter(endDate)) {
|
|
|
|
+ monthsBetween.add(currentMonth.format(formatter));
|
|
|
|
+ currentMonth = currentMonth.plusMonths(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return monthsBetween;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
- Date nowDate = DateUtils.getNowDate();
|
|
|
|
- Date date = plusDate(1, nowDate);
|
|
|
|
- Date date1 = plusDate(-1, nowDate);
|
|
|
|
- System.out.println(date);
|
|
|
|
- System.out.println(date1);
|
|
|
|
- /*System.out.println(getMonthsBefore("2024-02", 2));
|
|
|
|
- System.out.println();
|
|
|
|
- System.out.println(getNYearSameDay("2024-02-01", 1));
|
|
|
|
- System.out.println(getNYearSameMonth("2024-02", 1));
|
|
|
|
- List<String> allDatesInRangeDate = getAllDatesInRangeDate(date1, date);
|
|
|
|
- for (String s : allDatesInRangeDate) {
|
|
|
|
- System.out.println(s);
|
|
|
|
|
|
+ Date nowDate = getNowDate();
|
|
|
|
+ //近七日呼入电话量趋势
|
|
|
|
+ String date = DateUtils.getDate();
|
|
|
|
+ String sevenDaysAgo = DateUtils.plusDate(-DEFAULT_DAYS_QUERY_COUNT, nowDate, YYYY_MM_DD);
|
|
|
|
+ List<String> allDatesInRangeStr = DateUtils.getAllDatesInRangeStr(sevenDaysAgo, date);
|
|
|
|
+ for (String todayYearDate : allDatesInRangeStr) {
|
|
|
|
+ System.out.println(todayYearDate);
|
|
}
|
|
}
|
|
- System.out.println("------");
|
|
|
|
- try {
|
|
|
|
- String startDate = "2023-10-01";
|
|
|
|
- String endDate = "2023-10-10";
|
|
|
|
- List<String> dates = getAllDatesInRangeStr(startDate, endDate);
|
|
|
|
- for (String s : dates) {
|
|
|
|
- System.out.println(s);
|
|
|
|
- }
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }*/
|
|
|
|
- System.out.println(plusDate(-1, nowDate, YYYY_MM_DD));
|
|
|
|
|
|
+ System.out.println("11111111");
|
|
|
|
+ //近七日呼入电话量趋势--去年数据
|
|
|
|
+ String dateLastYear = DateUtils.getNYearSameDay(date, 1);
|
|
|
|
+ String sevenDaysAgoLastYear = DateUtils.getNYearSameDay(sevenDaysAgo, 1);
|
|
|
|
+ List<String> allDatesInRangeStrLastYear = DateUtils.getAllDatesInRangeStr(sevenDaysAgoLastYear, dateLastYear);
|
|
|
|
+ for (String lastYearDate : allDatesInRangeStrLastYear) {
|
|
|
|
+ System.out.println(lastYearDate);
|
|
|
|
+ }
|
|
|
|
+ System.out.println("22222222");
|
|
|
|
+ //近7个月电话量同比趋势
|
|
|
|
+ //获取当前的月份
|
|
|
|
+ String nowMonth = DateUtils.parseDateToStr(DateUtils.YYYY_MM, nowDate);
|
|
|
|
+ String sevenMonthAgo = DateUtils.getMonthsBefore(nowMonth, DEFAULT_DAYS_QUERY_COUNT);
|
|
|
|
+ List<String> todayMonths = DateUtils.getMonthsBetween(sevenMonthAgo, nowMonth);
|
|
|
|
+ for (String todayMonth : todayMonths) {
|
|
|
|
+ System.out.println(todayMonth);
|
|
|
|
+ }
|
|
|
|
+ System.out.println("333333333");
|
|
|
|
+ //近7个月电话量同比趋势--去年数据
|
|
|
|
+ String nowMonthLastYear = DateUtils.getNYearSameMonth(nowMonth, 1);
|
|
|
|
+ String sevenMonthAgoLastYear = DateUtils.getMonthsBefore(nowMonthLastYear, DEFAULT_DAYS_QUERY_COUNT);
|
|
|
|
+ List<String> lastYearMonths = DateUtils.getMonthsBetween(sevenMonthAgoLastYear, nowMonthLastYear);
|
|
|
|
+ for (String lastYearMonth : lastYearMonths) {
|
|
|
|
+ System.out.println(lastYearMonth);
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|