|
@@ -0,0 +1,318 @@
|
|
|
|
|
+package com.tzld.supply.util;
|
|
|
|
|
+
|
|
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
+
|
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
|
+import java.time.Instant;
|
|
|
|
|
+import java.time.LocalDate;
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
|
+import java.time.format.DateTimeParseException;
|
|
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * @author dyp
|
|
|
|
|
+ */
|
|
|
|
|
+public final class DateUtils {
|
|
|
|
|
+ public static String getCurrentDateStr(String format) {
|
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
|
|
+ Date currentDate = new Date();
|
|
|
|
|
+ return dateFormat.format(currentDate);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static int getCurrentHour() {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ return calendar.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getBeforeDaysDateStr(String format, int d) {
|
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ calendar.setTime(new Date());
|
|
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -d);
|
|
|
|
|
+ Date previousDate = calendar.getTime();
|
|
|
|
|
+ return dateFormat.format(previousDate);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getBeforeDaysDateStr(String dateStr, String format, int d) {
|
|
|
|
|
+ SimpleDateFormat dateFormat = new SimpleDateFormat(format);
|
|
|
|
|
+ Date date = new Date();
|
|
|
|
|
+ try {
|
|
|
|
|
+ date = dateFormat.parse(dateStr);
|
|
|
|
|
+ } catch (Exception ignore) {}
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -d);
|
|
|
|
|
+ Date previousDate = calendar.getTime();
|
|
|
|
|
+ return dateFormat.format(previousDate);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static boolean ifTimeRangeInNow(String timeRange) {
|
|
|
|
|
+ try {
|
|
|
|
|
+
|
|
|
|
|
+ String[] split = timeRange.split("-");
|
|
|
|
|
+ if (split.length != 2) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ String startTime = split[0];
|
|
|
|
|
+ String endTime = split[1];
|
|
|
|
|
+ if (startTime.length() != 10 || endTime.length() != 10) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
+ LocalDateTime start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyyMMddHH"));
|
|
|
|
|
+ LocalDateTime end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyyMMddHH"));
|
|
|
|
|
+ return now.equals(start) || (now.isAfter(start) && now.isBefore(end));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+// e.printStackTrace();
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static long dateStrToTimestamp(String dateStr, String format) {
|
|
|
|
|
+ // 定义日期格式
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
|
|
|
+
|
|
|
|
|
+ // 解析输入的日期字符串为LocalDate对象
|
|
|
|
|
+ LocalDate date = LocalDate.parse(dateStr, formatter);
|
|
|
|
|
+
|
|
|
|
|
+ // 将LocalDate转换为LocalDateTime,时间部分设为00:00:00
|
|
|
|
|
+ LocalDateTime dateTime = date.atStartOfDay();
|
|
|
|
|
+
|
|
|
|
|
+ // 定义北京时区
|
|
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
|
|
+
|
|
|
|
|
+ // 将LocalDateTime转换为时间戳(秒数),使用北京时区
|
|
|
|
|
+ return dateTime.toEpochSecond(zone.getRules().getOffset(dateTime));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static boolean isValidDate(String dateString) {
|
|
|
|
|
+ // 定义日期格式
|
|
|
|
|
+ DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 尝试解析日期字符串
|
|
|
|
|
+ LocalDate date = LocalDate.parse(dateString, dateFormatter);
|
|
|
|
|
+ return true; // 如果解析成功,返回 true
|
|
|
|
|
+ } catch (DateTimeParseException e) {
|
|
|
|
|
+ return false; // 如果解析失败,返回 false
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String timestampToYMDStr(Long timestamp, String format) {
|
|
|
|
|
+
|
|
|
|
|
+ // 定义北京时区
|
|
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
|
|
+
|
|
|
|
|
+ // 将时间戳转换为LocalDateTime
|
|
|
|
|
+ LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), zone);
|
|
|
|
|
+
|
|
|
|
|
+ // 定义日期格式为"yyyyMMddHHmmss"
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
|
|
|
+
|
|
|
|
|
+ // 将LocalDateTime格式化为字符串
|
|
|
|
|
+ return dateTime.format(formatter);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String findNearestDate(List<String> dateList, String targetDateStr, String format) {
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
|
|
|
+ // 将目标日期字符串转换为 LocalDate
|
|
|
|
|
+ LocalDate targetDate = LocalDate.parse(targetDateStr, formatter);
|
|
|
|
|
+
|
|
|
|
|
+ // 初始化最近的日期和最小的天数差值
|
|
|
|
|
+ String nearestDateStr = null;
|
|
|
|
|
+ long minDiff = Long.MAX_VALUE;
|
|
|
|
|
+
|
|
|
|
|
+ // 遍历日期列表,找到最近的日期
|
|
|
|
|
+ for (String dateStr : dateList) {
|
|
|
|
|
+ LocalDate currentDate = LocalDate.parse(dateStr, formatter);
|
|
|
|
|
+ long diff = Math.abs(ChronoUnit.DAYS.between(currentDate, targetDate)); // 计算天数差异
|
|
|
|
|
+
|
|
|
|
|
+ // 如果找到更小的差异,更新最近的日期
|
|
|
|
|
+ if (diff < minDiff) {
|
|
|
|
|
+ minDiff = diff;
|
|
|
|
|
+ nearestDateStr = dateStr;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return nearestDateStr; // 返回最近的日期字符串
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static List<String> getBeforeDays(String beginDate, String endDate, int days) {
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
|
|
+ // 获取今天的日期
|
|
|
|
|
+ LocalDate today = LocalDate.now();
|
|
|
|
|
+ if (!StringUtils.hasText(beginDate)) {
|
|
|
|
|
+ beginDate = today.format(formatter);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StringUtils.hasText(endDate)) {
|
|
|
|
|
+ endDate = today.format(formatter);
|
|
|
|
|
+ }
|
|
|
|
|
+ // 解析输入的日期字符串为LocalDate对象
|
|
|
|
|
+ LocalDate date = LocalDate.parse(beginDate, formatter);
|
|
|
|
|
+ LocalDate end = LocalDate.parse(endDate, formatter);
|
|
|
|
|
+ // 获取从输入日期前三天的日期
|
|
|
|
|
+ LocalDate startDate = date.minusDays(days);
|
|
|
|
|
+ // 存储所有日期的列表
|
|
|
|
|
+ List<String> datesList = new ArrayList<>();
|
|
|
|
|
+ // 从startDate到today遍历日期
|
|
|
|
|
+ while (startDate.isBefore(today) && !startDate.isAfter(end)) {
|
|
|
|
|
+ // 将当前日期格式化为"yyyyMMdd"并添加到列表中
|
|
|
|
|
+ datesList.add(startDate.format(formatter));
|
|
|
|
|
+ // 日期加1天
|
|
|
|
|
+ startDate = startDate.plusDays(1);
|
|
|
|
|
+ }
|
|
|
|
|
+ return datesList;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取每日最小秒数 (当天零点)
|
|
|
|
|
+ public static Long getStartOfDay(String dateStr, String formatter) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(formatter);
|
|
|
|
|
+ Date date = sdf.parse(dateStr);
|
|
|
|
|
+
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
+
|
|
|
|
|
+ return calendar.getTimeInMillis() / 1000;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Date date = new Date();
|
|
|
|
|
+ return date.getTime() / 1000;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Date getStartDateOfDay(Long timestamp) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ // 设置时区为UTC(你可以根据需要更改时区)
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ // 将时间戳转换为毫秒并设置时间
|
|
|
|
|
+ calendar.setTimeInMillis(timestamp * 1000L);
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
+
|
|
|
|
|
+ return calendar.getTime();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ return new Date();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取每日最大秒数 (当天的23:59:59)
|
|
|
|
|
+ public static Long getEndOfDay(String dateStr, String formatter) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(formatter);
|
|
|
|
|
+ Date date = sdf.parse(dateStr);
|
|
|
|
|
+
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ calendar.setTime(date);
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 23);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 59);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 59);
|
|
|
|
|
+ calendar.set(Calendar.MILLISECOND, 999);
|
|
|
|
|
+
|
|
|
|
|
+ return calendar.getTimeInMillis() / 1000;
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ Date date = new Date();
|
|
|
|
|
+ return date.getTime() / 1000;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Integer getHourByTimestamp(Long timestamp) {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ // 设置时区为UTC(你可以根据需要更改时区)
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ // 将时间戳转换为毫秒并设置时间
|
|
|
|
|
+ calendar.setTimeInMillis(timestamp * 1000L);
|
|
|
|
|
+ // 获取小时
|
|
|
|
|
+ return calendar.get(Calendar.HOUR_OF_DAY);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static int getMinuteByTimestamp(long timestamp) {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ // 设置时区为UTC(你可以根据需要更改时区)
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ // 将时间戳转换为毫秒并设置时间
|
|
|
|
|
+ calendar.setTimeInMillis(timestamp * 1000L);
|
|
|
|
|
+ // 获取小时
|
|
|
|
|
+ return calendar.get(Calendar.MINUTE);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+
|
|
|
|
|
+ System.out.println((calendar.get(Calendar.DAY_OF_WEEK) + 6) % 7);
|
|
|
|
|
+ System.out.println(new SimpleDateFormat("yyyyMMdd").format(calendar.getTime()));
|
|
|
|
|
+ System.out.println(new SimpleDateFormat("HH").format(calendar.getTime()));
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Long getTodayStart() {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
+ return calendar.getTime().getTime();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Long getBeforeDayStart(int days) {
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
|
|
|
|
|
+ calendar.set(Calendar.HOUR_OF_DAY, 0);
|
|
|
|
|
+ calendar.set(Calendar.MINUTE, 0);
|
|
|
|
|
+ calendar.set(Calendar.SECOND, 0);
|
|
|
|
|
+ calendar.set(Calendar.MILLISECOND, 0);
|
|
|
|
|
+ calendar.add(Calendar.DAY_OF_MONTH, -days);
|
|
|
|
|
+ return calendar.getTime().getTime() / 1000;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getBeforeDayStr(String dateStr, String format, int days) {
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
|
|
|
|
|
+ LocalDate date = LocalDate.parse(dateStr, formatter);
|
|
|
|
|
+ date = date.minusDays(days);
|
|
|
|
|
+ return date.format(formatter);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getDateString(Long timestamp) {
|
|
|
|
|
+ // 创建日期时间格式化器
|
|
|
|
|
+ DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("yyyyMMdd");
|
|
|
|
|
+ // 将时间戳转换为 LocalDateTime
|
|
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
|
|
+ LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);
|
|
|
|
|
+ // 格式化日期时间并返回
|
|
|
|
|
+ return dateTime.format(dateFormat);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static String getDateString(Long timestamp, String pattern) {
|
|
|
|
|
+ // 创建日期时间格式化器
|
|
|
|
|
+ DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(pattern);
|
|
|
|
|
+ // 将时间戳转换为 LocalDateTime
|
|
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
|
|
+ LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zone);
|
|
|
|
|
+ // 格式化日期时间并返回
|
|
|
|
|
+ return dateTime.format(dateFormat);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public static Date getDate(String dateString) {
|
|
|
|
|
+ DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
|
|
|
|
+ LocalDate localDate = LocalDate.parse(dateString, formatter);
|
|
|
|
|
+ ZoneId zone = ZoneId.of("Asia/Shanghai");
|
|
|
|
|
+ return Date.from(localDate.atStartOfDay(zone).toInstant());
|
|
|
|
|
+ }
|
|
|
|
|
+}
|