|
@@ -2,6 +2,9 @@ package com.ywt.biz.common.util;
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.LocalDate;
|
|
|
+import java.time.ZoneId;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
import java.util.GregorianCalendar;
|
|
@@ -237,6 +240,143 @@ public class DateUtil {
|
|
|
return sdf.format(date);
|
|
|
}
|
|
|
|
|
|
+ public static LocalDate convertToLocalDate(Date date) {
|
|
|
+ Instant instant = date.toInstant();
|
|
|
+ ZoneId zoneId = ZoneId.systemDefault();
|
|
|
+ return instant.atZone(zoneId).toLocalDate();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将时间戮转换成 LocalDate 类型
|
|
|
+ *
|
|
|
+ * @param timestamp 时间戮
|
|
|
+ * @return 日期类型 LocalDate
|
|
|
+ */
|
|
|
+ public static LocalDate convertToLocalDate(long timestamp) {
|
|
|
+ return Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDate();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到距离当前的天数
|
|
|
+ *
|
|
|
+ * @param beginDateStr 开始日期
|
|
|
+ * @return 天数
|
|
|
+ */
|
|
|
+ public static long getCurrentDay(String beginDateStr) {
|
|
|
+ return getDaySub(beginDateStr, new SimpleDateFormat(DADE_FROMAT_YMD).format(new Date()));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到相减的天数
|
|
|
+ *
|
|
|
+ * @param beginDateStr 开始时间
|
|
|
+ * @param endDateStr 截止时间
|
|
|
+ * @return 天数
|
|
|
+ */
|
|
|
+ public static long getDaySub(String beginDateStr, String endDateStr) {
|
|
|
+ long day = 0;
|
|
|
+ java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(DADE_FROMAT_YMD);
|
|
|
+ java.util.Date beginDate;
|
|
|
+ java.util.Date endDate;
|
|
|
+ try {
|
|
|
+ beginDate = format.parse(beginDateStr);
|
|
|
+ endDate = format.parse(endDateStr);
|
|
|
+ day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ // TODO 自动生成 catch 块
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改日期,得到当前
|
|
|
+ *
|
|
|
+ * @param startDate 开始的日期
|
|
|
+ * @param num 修改的天数
|
|
|
+ * @return 修改后的日期 {@link Date}
|
|
|
+ */
|
|
|
+ public static Date getChangeDayOfDate(String startDate, int num, String pattern) {
|
|
|
+ Date date = stringToDate(startDate, pattern);
|
|
|
+ Calendar ca = Calendar.getInstance();
|
|
|
+ ca.setTime(date);
|
|
|
+ ca.add(Calendar.DAY_OF_YEAR, num);
|
|
|
+ //最后一天格式化
|
|
|
+ return ca.getTime();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到相减的天数(精确到时分秒)
|
|
|
+ *
|
|
|
+ * @param beginDateStr 开始时间
|
|
|
+ * @param endDateStr 截止时间
|
|
|
+ * @return 天数
|
|
|
+ */
|
|
|
+ public static long getDaySubByYMDMS(String beginDateStr, String endDateStr) {
|
|
|
+ long day = 0;
|
|
|
+ java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(DADE_FROMAT_YMDHMS);
|
|
|
+ java.util.Date beginDate;
|
|
|
+ java.util.Date endDate;
|
|
|
+ try {
|
|
|
+ beginDate = format.parse(beginDateStr);
|
|
|
+ endDate = format.parse(endDateStr);
|
|
|
+ day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ // TODO 自动生成 catch 块
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return day;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>Checks if two calendars represent the same day ignoring time.</p>
|
|
|
+ *
|
|
|
+ * @param cal1 the first calendar, not altered, not null
|
|
|
+ * @param cal2 the second calendar, not altered, not null
|
|
|
+ * @return true if they represent the same day
|
|
|
+ * @throws IllegalArgumentException if either calendar is <code>null</code>
|
|
|
+ */
|
|
|
+ public static boolean isSameDay(Calendar cal1, Calendar cal2) {
|
|
|
+ if (cal1 == null || cal2 == null) {
|
|
|
+ throw new IllegalArgumentException("The dates must not be null");
|
|
|
+ }
|
|
|
+ return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
|
|
|
+ cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
|
|
|
+ cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>Checks if two dates are on the same day ignoring time.</p>
|
|
|
+ *
|
|
|
+ * @param date1 the first date, not altered, not null
|
|
|
+ * @param date2 the second date, not altered, not null
|
|
|
+ * @return true if they represent the same day
|
|
|
+ * @throws IllegalArgumentException if either date is <code>null</code>
|
|
|
+ */
|
|
|
+ public static boolean isSameDay(Date date1, Date date2) throws IllegalArgumentException {
|
|
|
+ if (date1 == null || date2 == null) {
|
|
|
+ throw new IllegalArgumentException("The dates must not be null");
|
|
|
+ }
|
|
|
+ Calendar cal1 = Calendar.getInstance();
|
|
|
+ cal1.setTime(date1);
|
|
|
+ Calendar cal2 = Calendar.getInstance();
|
|
|
+ cal2.setTime(date2);
|
|
|
+ return isSameDay(cal1, cal2);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * <p>Checks if a date is today.</p>
|
|
|
+ *
|
|
|
+ * @param date the date, not altered, not null.
|
|
|
+ * @return true if the date is today.
|
|
|
+ * @throws IllegalArgumentException if the date is <code>null</code>
|
|
|
+ */
|
|
|
+ public static boolean isToday(Date date) throws IllegalArgumentException {
|
|
|
+ return isSameDay(date, Calendar.getInstance().getTime());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
public static boolean isValidFormat(String format, String value) {
|
|
|
Date date = null;
|
|
|
try {
|