|
@@ -15,6 +15,7 @@ import java.util.List;
|
|
|
|
|
|
public class DateUtils {
|
|
|
public static final String SDFSECOND = "yyyyMMddHHmmss";
|
|
|
+ public static final String SDFDAY = "yyyyMMdd";
|
|
|
public static final String SDF_SECOND = "yyyy-MM-dd HH:mm:ss";
|
|
|
|
|
|
/**
|
|
@@ -193,6 +194,9 @@ public class DateUtils {
|
|
|
* @version V1.0 2021/5/30 11:43 上午
|
|
|
*/
|
|
|
public static Date getFirstDayOfLastYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
Instant instant = date.toInstant();
|
|
|
ZoneId zoneId = ZoneId.systemDefault();
|
|
|
|
|
@@ -203,6 +207,24 @@ public class DateUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * date转换为localDate
|
|
|
+ *
|
|
|
+ * @param date Date对象
|
|
|
+ * @return LocalDate对象
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/6/3 11:36 上午
|
|
|
+ */
|
|
|
+ public static LocalDate convert2LocalDate(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ return instant.atZone(zoneId).toLocalDate();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 去年十二月第一天
|
|
|
*
|
|
|
* @param date 参照的日期
|
|
@@ -211,6 +233,9 @@ public class DateUtils {
|
|
|
* @version V1.0 2021/5/30 11:43 上午
|
|
|
*/
|
|
|
public static Date getDecemberFirstDayOfLastYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
Instant instant = date.toInstant();
|
|
|
ZoneId zoneId = ZoneId.systemDefault();
|
|
|
|
|
@@ -221,6 +246,28 @@ public class DateUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 去年同期月份第一天
|
|
|
+ *
|
|
|
+ * @param date 参照的日期
|
|
|
+ * @return 去年同期月份第一天
|
|
|
+ * @author lixing
|
|
|
+ * @version V1.0 2021/5/30 11:43 上午
|
|
|
+ */
|
|
|
+ public static Date getSameMonthFirstDayOfLastYear(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+
|
|
|
+ LocalDate localDate = instant.atZone(zoneId).toLocalDate();
|
|
|
+ LocalDate lastYear = localDate.minusYears(1);
|
|
|
+ LocalDate lastYearFirstDay = LocalDate.of(lastYear.getYear(), lastYear.getMonth(), 1);
|
|
|
+ return Date.from(lastYearFirstDay.atStartOfDay(zoneId).toInstant());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
* 获取阶段内的每个月的第一天
|
|
|
*
|
|
|
* @param startDate 开始日期
|
|
@@ -279,6 +326,9 @@ public class DateUtils {
|
|
|
* @version V1.0 2021/5/30 11:44 上午
|
|
|
*/
|
|
|
public static Date getFirstDayOfLastMonth(Date date) {
|
|
|
+ if (date == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
Instant instant = date.toInstant();
|
|
|
ZoneId zoneId = ZoneId.systemDefault();
|
|
|
|
|
@@ -316,7 +366,7 @@ public class DateUtils {
|
|
|
// 2020-02-01
|
|
|
Date startDate = new Date(1580486400000L);
|
|
|
Date endDate = new Date();
|
|
|
- System.out.println(getFirstDayOfEveryMonth(startDate, endDate));
|
|
|
+ System.out.println(getSameMonthFirstDayOfLastYear(startDate));
|
|
|
}
|
|
|
|
|
|
}
|