|
@@ -7,10 +7,7 @@ import java.text.SimpleDateFormat;
|
|
|
import java.time.*;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.format.DateTimeFormatterBuilder;
|
|
|
-import java.time.temporal.Temporal;
|
|
|
-import java.time.temporal.TemporalAdjusters;
|
|
|
-import java.time.temporal.TemporalUnit;
|
|
|
-import java.time.temporal.WeekFields;
|
|
|
+import java.time.temporal.*;
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
@@ -24,7 +21,7 @@ import java.util.*;
|
|
|
@UtilityClass
|
|
|
public class DateUtils {
|
|
|
public final static String PATTERN_YMD_HMS = "yyyy-MM-dd HH:mm:ss";
|
|
|
-
|
|
|
+ public final static String PATTERN_YM = "yyyy-MM";
|
|
|
public final static String PATTERN_YMD = "yyyy-MM-dd";
|
|
|
public final static String PATTERN_YMD_DOT = "yyyy.MM.dd";
|
|
|
public final static String PATTERN_YMD_ZH = "yyyy年MM月dd日";
|
|
@@ -134,6 +131,17 @@ public class DateUtils {
|
|
|
return firstday;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取当前月第一天:00:00:00
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getFirstDayOfMonth(Integer year, Integer month) {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ LocalDateTime firstday = now.withYear(year).withMonth(month).with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
|
|
|
+ return firstday;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前月最后一天:23:59:59
|
|
|
*
|
|
@@ -145,6 +153,38 @@ public class DateUtils {
|
|
|
return lastDay ;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取当前月最后一天:23:59:59
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getLastDayOfMonth(Integer year, Integer month) {
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+ LocalDateTime lastDay = now.withYear(year).withMonth(month).with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
|
|
|
+ return lastDay ;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 指定年月的最后一天开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getFirstDayOfMonth(LocalDate month) {
|
|
|
+ LocalDateTime day = month.atStartOfDay();
|
|
|
+ LocalDateTime firstday = day.with(TemporalAdjusters.firstDayOfMonth()).withHour(0).withMinute(0).withSecond(0);
|
|
|
+ return firstday;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 指定月份的第一天开始时间
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getLastDayOfMonth(LocalDate month) {
|
|
|
+ LocalDateTime day = month.atStartOfDay();
|
|
|
+ LocalDateTime lastDay = day.with(TemporalAdjusters.lastDayOfMonth()).withHour(23).withMinute(59).withSecond(59);
|
|
|
+ return lastDay;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取某年,某周的第一天
|
|
|
*
|
|
@@ -381,10 +421,17 @@ public class DateUtils {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- List<Map<String, LocalDateTime>> monthStartAndEndList = getWeekStartAndEndByYear(2021);
|
|
|
+ //List<Map<String, LocalDateTime>> monthStartAndEndList = getMonthStartAndEndByYear(2021);
|
|
|
+
|
|
|
+ List<Map<String, LocalDateTime>> monthStartAndEndList = getMonthStartAndEndByYear(LocalDate.now().withYear(2020).withMonth(2), LocalDate.now().withYear(2021).withMonth(8));
|
|
|
for(Map<String, LocalDateTime> map: monthStartAndEndList){
|
|
|
- System.out.println(map.get("searchStartTimeWeek").toString() + " : " + map.get("searchEndTimeWeek").toString());
|
|
|
+ System.out.println(map.get("searchStartTimeMonth").toString() + " : " + map.get("searchEndTimeMonth").toString());
|
|
|
}
|
|
|
+ /*LocalDate nowDate = LocalDate.now();
|
|
|
+ LocalDateTime time = getFirstDayOfMonth(nowDate);
|
|
|
+ System.out.println(time);
|
|
|
+ LocalDateTime time2 = getLastDayOfMonth(nowDate);
|
|
|
+ System.out.println(time2);*/
|
|
|
}
|
|
|
/**
|
|
|
* date 转 LocalDate
|
|
@@ -408,8 +455,30 @@ public class DateUtils {
|
|
|
// 每年的 一月一号,零时零分零秒
|
|
|
for(int i = 1;i<month;i++){
|
|
|
Map<String, LocalDateTime> dateTimeMap = new HashMap<String, LocalDateTime>();
|
|
|
- LocalDateTime searchStartTimeMonth = getFirstDayOfMonth(i);
|
|
|
- LocalDateTime searchEndTimeMonth = getLastDayOfMonth(i);
|
|
|
+ LocalDateTime searchStartTimeMonth = getFirstDayOfMonth(year, i);
|
|
|
+ LocalDateTime searchEndTimeMonth = getLastDayOfMonth(year, i);
|
|
|
+ dateTimeMap.put("searchStartTimeMonth", searchStartTimeMonth);
|
|
|
+ dateTimeMap.put("searchEndTimeMonth",searchEndTimeMonth);
|
|
|
+ monthStartAndEndList.add(dateTimeMap);
|
|
|
+ }
|
|
|
+ return monthStartAndEndList;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 指定年份开始年月到结束年月,中间每个月的开始时间和结束时间
|
|
|
+ *
|
|
|
+ * @param startMonth
|
|
|
+ * @param endMonth
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Map<String, LocalDateTime>> getMonthStartAndEndByYear(LocalDate startMonth, LocalDate endMonth) {
|
|
|
+ List<Map<String, LocalDateTime>> monthStartAndEndList = new ArrayList<Map<String,LocalDateTime>>();
|
|
|
+ int month = DateUtils.monthDiff(startMonth, endMonth);
|
|
|
+ for(int i = 0;i<=month;i++){
|
|
|
+ LocalDate date = DateUtils.plus(startMonth, i, ChronoUnit.MONTHS);
|
|
|
+ Map<String, LocalDateTime> dateTimeMap = new HashMap<String, LocalDateTime>();
|
|
|
+ LocalDateTime searchStartTimeMonth = getFirstDayOfMonth(date);
|
|
|
+ LocalDateTime searchEndTimeMonth = getLastDayOfMonth(date);
|
|
|
dateTimeMap.put("searchStartTimeMonth", searchStartTimeMonth);
|
|
|
dateTimeMap.put("searchEndTimeMonth",searchEndTimeMonth);
|
|
|
monthStartAndEndList.add(dateTimeMap);
|