|
@@ -6,9 +6,11 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
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.util.*;
|
|
|
|
|
|
|
|
@@ -143,6 +145,51 @@ public class DateUtils {
|
|
|
return lastDay ;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取某年,某周的第一天
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * @param week
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getFirstDayOfWeek(int year, int week) {
|
|
|
+ WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,1);
|
|
|
+ LocalDateTime ldt = LocalDateTime.now()
|
|
|
+ .withYear(year)
|
|
|
+ .with(weekFields.weekOfYear(), week)
|
|
|
+ .with(weekFields.dayOfWeek(), 1).withHour(0).withMinute(0).withSecond(0);
|
|
|
+ return ldt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某年,某周的最后一天
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * @param week
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getLastDayOfWeek(int year, int week) {
|
|
|
+ WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,1);
|
|
|
+ LocalDateTime ldt = LocalDateTime.now()
|
|
|
+ .withYear(year)
|
|
|
+ .with(weekFields.weekOfYear(), week)
|
|
|
+ .with(weekFields.dayOfWeek(), 7).withHour(23).withMinute(59).withSecond(59);
|
|
|
+ return ldt;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取某年,最后一周的最后一天
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static LocalDateTime getLastDayOfWeekOfYear(int year) {
|
|
|
+ WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,1);
|
|
|
+ LocalDateTime lastDayOfYear = DateUtils.getLastDayOfThisYear(year);
|
|
|
+ Integer lastWeek = lastDayOfYear.get(weekFields.weekOfYear());
|
|
|
+ return getLastDayOfWeek(year, lastWeek);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 是否是闰年
|
|
|
*
|
|
@@ -334,9 +381,9 @@ public class DateUtils {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- List<Map<String, LocalDateTime>> monthStartAndEndList = getMonthStartAndEndByYear(2021,10);
|
|
|
+ List<Map<String, LocalDateTime>> monthStartAndEndList = getWeekStartAndEndByYear(2021);
|
|
|
for(Map<String, LocalDateTime> map: monthStartAndEndList){
|
|
|
- System.out.println(map.get("searchStartTimeMonth").toString() + " : " + map.get("searchEndTimeMonth").toString());
|
|
|
+ System.out.println(map.get("searchStartTimeWeek").toString() + " : " + map.get("searchEndTimeWeek").toString());
|
|
|
}
|
|
|
}
|
|
|
/**
|
|
@@ -350,7 +397,7 @@ public class DateUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 制定年份,月份之前的每个月的开始时间和结束时间
|
|
|
+ * 指定年份,一直到指定月份的每个月的开始时间和结束时间
|
|
|
*
|
|
|
* @param year
|
|
|
* @param month
|
|
@@ -358,9 +405,7 @@ public class DateUtils {
|
|
|
*/
|
|
|
public static List<Map<String, LocalDateTime>> getMonthStartAndEndByYear(int year, int month) {
|
|
|
List<Map<String, LocalDateTime>> monthStartAndEndList = new ArrayList<Map<String,LocalDateTime>>();
|
|
|
- LocalDateTime now = LocalDateTime.now();
|
|
|
// 每年的 一月一号,零时零分零秒
|
|
|
- LocalDateTime startDay = now.withYear(year).with(TemporalAdjusters.firstDayOfYear()).withHour(0).withMinute(0).withSecond(0);
|
|
|
for(int i = 1;i<month;i++){
|
|
|
Map<String, LocalDateTime> dateTimeMap = new HashMap<String, LocalDateTime>();
|
|
|
LocalDateTime searchStartTimeMonth = getFirstDayOfMonth(i);
|
|
@@ -372,5 +417,33 @@ public class DateUtils {
|
|
|
return monthStartAndEndList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 指定年份,每周的开始时间和结束时间
|
|
|
+ *
|
|
|
+ * @param year
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<Map<String, LocalDateTime>> getWeekStartAndEndByYear(int year) {
|
|
|
+ List<Map<String, LocalDateTime>> weekStartAndEndList = new ArrayList<Map<String,LocalDateTime>>();
|
|
|
+ WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,1);
|
|
|
+ LocalDate now = LocalDate.now();
|
|
|
+ LocalDateTime firstDayOfYear = DateUtils.getFirstDayOfThisYear(year);
|
|
|
+ LocalDateTime lastDayOfYear = DateUtils.getLastDayOfThisYear(year);
|
|
|
+ //获取第一周和最后一周
|
|
|
+ Integer firstWeek = firstDayOfYear.get(weekFields.weekOfYear());
|
|
|
+ Integer lastWeek = lastDayOfYear.get(weekFields.weekOfYear());
|
|
|
+ // 每年的 一月一号,零时零分零秒
|
|
|
+ for(int i = firstWeek;i<lastWeek;i++){
|
|
|
+ Map<String, LocalDateTime> dateTimeMap = new HashMap<String, LocalDateTime>();
|
|
|
+ LocalDateTime searchStartTimeWeek = getFirstDayOfWeek(year, i);
|
|
|
+ LocalDateTime searchEndTimeWeek = getLastDayOfWeek(year, i);
|
|
|
+ dateTimeMap.put("searchStartTimeWeek", searchStartTimeWeek);
|
|
|
+ dateTimeMap.put("searchEndTimeWeek",searchEndTimeWeek);
|
|
|
+ weekStartAndEndList.add(dateTimeMap);
|
|
|
+ }
|
|
|
+ return weekStartAndEndList;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|