This commit is contained in:
ls
2024-12-23 14:37:37 +08:00
parent 4b1b383001
commit d3b5631c9e
5 changed files with 152 additions and 101 deletions

View File

@@ -1,21 +1,15 @@
package org.jeecg.common.util;
import org.jeecg.common.constant.SymbolConstant;
import org.jeecg.common.util.date.TimeRange;
import org.springframework.util.StringUtils;
import java.beans.PropertyEditorSupport;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.text.*;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.*;
/**
* 类描述:时间操作定义类
@@ -26,25 +20,25 @@ import java.util.GregorianCalendar;
*/
public class DateUtils extends PropertyEditorSupport {
public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() {
public static ThreadLocal<SimpleDateFormat> date_sdf = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() {
public static ThreadLocal<SimpleDateFormat> yyyyMMdd = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyyMMdd");
}
};
public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() {
public static ThreadLocal<SimpleDateFormat> date_sdf_wz = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy年MM月dd日");
}
};
public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() {
public static ThreadLocal<SimpleDateFormat> time_sdf = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm");
@@ -72,8 +66,8 @@ public class DateUtils extends PropertyEditorSupport {
/**
* 以毫秒表示的时间
*/
private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
private static final long HOUR_IN_MILLIS = 3600 * 1000;
private static final long DAY_IN_MILLIS = 24 * 3600 * 1000;
private static final long HOUR_IN_MILLIS = 3600 * 1000;
private static final long MINUTE_IN_MILLIS = 60 * 1000;
private static final long SECOND_IN_MILLIS = 1000;
@@ -123,7 +117,6 @@ public class DateUtils extends PropertyEditorSupport {
return new Date();
}
/**
* 当前日期
*
@@ -816,4 +809,57 @@ public class DateUtils extends PropertyEditorSupport {
return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR);
}
/**
* 找出列表中最早的时间,比较开始时间和结束时间,返回最早的
*
* @param timeRanges
* @return
*/
public static Date findEarliestTime(List<TimeRange> timeRanges) {
if (timeRanges == null || timeRanges.isEmpty()) {
return null; // 处理空列表的情况
}
return timeRanges.stream()
//过滤掉start或end为null 的数据
.filter(timeRange -> timeRange.getStartTime() != null || timeRange.getEndTime() != null)
.flatMap(timeRange -> {
List<Date> dates = new ArrayList<>();
if (timeRange.getStartTime() != null) {
dates.add(timeRange.getStartTime());
}
if (timeRange.getEndTime() != null) {
dates.add(timeRange.getEndTime());
}
return dates.stream();
})
.min(Date::compareTo)
.orElse(null);
}
/**
* 找出列表中最晚的时间,比较开始时间和结束时间,返回最晚的
*
* @param timeRanges
* @return
*/
public static Date findLatestTime(List<TimeRange> timeRanges) {
if (timeRanges == null || timeRanges.isEmpty()) {
return null; // 处理空列表的情况
}
return timeRanges.stream()
//过滤掉start或end为null 的数据
.filter(timeRange -> timeRange.getStartTime() != null || timeRange.getEndTime() != null)
.flatMap(timeRange -> {
List<Date> dates = new ArrayList<>();
if (timeRange.getStartTime() != null) {
dates.add(timeRange.getStartTime());
}
if (timeRange.getEndTime() != null) {
dates.add(timeRange.getEndTime());
}
return dates.stream();
})
.max(Date::compareTo)
.orElse(null);
}
}

View File

@@ -0,0 +1,22 @@
/*
* Ant Group
* Copyright (c) 2004-2024 All Rights Reserved.
*/
package org.jeecg.common.util.date;
import lombok.*;
import java.util.Date;
/**
* @author lise
* @version TimeRange.java, v 0.1 2024年12月23日 14:21 lise
*/
@Getter
@Setter
@AllArgsConstructor
public class TimeRange {
private Date startTime;
private Date endTime;
}