830 lines
30 KiB
Java
Raw Normal View History

2023-02-16 15:28:15 +08:00
package com.zhgd.xmgl.util;
2023-10-31 18:59:50 +08:00
2023-06-02 13:59:52 +08:00
import cn.hutool.core.date.DateTime;
2023-07-29 14:41:03 +08:00
import cn.hutool.core.date.DateUnit;
2023-10-31 17:45:22 +08:00
import cn.hutool.core.date.DateUtil;
2025-04-25 18:09:21 +08:00
import cn.hutool.core.util.StrUtil;
2024-04-10 13:36:06 +08:00
import com.zhgd.jeecg.common.execption.OpenAlertException;
2024-04-14 21:05:01 +08:00
import lombok.extern.slf4j.Slf4j;
2024-04-10 13:36:06 +08:00
import org.apache.commons.lang3.time.DateFormatUtils;
2023-06-02 13:59:52 +08:00
2023-06-07 11:26:18 +08:00
import java.text.DateFormat;
2023-02-16 15:28:15 +08:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
2025-04-25 18:09:21 +08:00
import java.time.*;
2023-02-16 15:28:15 +08:00
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
2023-06-05 19:02:07 +08:00
import java.util.*;
2024-03-19 10:28:12 +08:00
import java.util.regex.Matcher;
import java.util.regex.Pattern;
2023-06-02 13:59:52 +08:00
2023-02-16 15:28:15 +08:00
/**
* @program: wisdomSite
* @description: 日期格式
* @author: Mr.Peng
* @create: 2020-12-15 17:05
**/
2024-04-14 21:05:01 +08:00
@Slf4j
2023-10-31 17:45:22 +08:00
public class DateUtils {
2025-04-25 18:09:21 +08:00
/**
* 验证日期段格式日期格式yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd yyyy年MM月dd日分割符号~
*
* @param input
* @return
*/
public static boolean validateDateRange(String input) {
if (StrUtil.isBlank(input)) {
return true;
}
return parseDateRange(input) != null;
}
/**
* 解析日期段格式日期格式yyyy-MM-dd yyyy/MM/dd yyyy.MM.dd yyyy年MM月dd日分割符号~
*
* @param input
* @return
*/
public static List<Date> parseDateRange(String input) {
input = StrUtil.trim(input);
String regex = "^\\s*" +
"(?:" +
// 标准格式yyyy-MM-dd 或 yyyy/MM/dd 或 yyyy.MM.dd
"(?<stdStartY>\\d{4})[-/.]?(?<stdStartM>\\d{1,2})[-/.]?(?<stdStartD>\\d{1,2})" +
"|" +
// 中文格式yyyy年MM月dd日
"(?<cnStartY>\\d{4})\\s*年\\s*(?<cnStartM>\\d{1,2})\\s*月\\s*(?<cnStartD>\\d{1,2})\\s*日" +
")" +
"\\s*" +
// 分隔符
"(?<sep>到|至|——|—|~|--+)" +
"\\s*" +
"(?:" +
// 标准格式结束日期
"(?<stdEndY>\\d{4})[-/.]?(?<stdEndM>\\d{1,2})[-/.]?(?<stdEndD>\\d{1,2})" +
"|" +
// 中文格式结束日期
"(?<cnEndY>\\d{4})\\s*年\\s*(?<cnEndM>\\d{1,2})\\s*月\\s*(?<cnEndD>\\d{1,2})\\s*日" +
")" +
"\\s*$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
try {
// 解析开始日期
int startYear, startMonth, startDay;
if (matcher.group("stdStartY") != null) {
// 标准格式
startYear = Integer.parseInt(matcher.group("stdStartY"));
startMonth = Integer.parseInt(matcher.group("stdStartM"));
startDay = Integer.parseInt(matcher.group("stdStartD"));
} else {
// 中文格式
startYear = Integer.parseInt(matcher.group("cnStartY"));
startMonth = Integer.parseInt(matcher.group("cnStartM"));
startDay = Integer.parseInt(matcher.group("cnStartD"));
}
// 解析结束日期
int endYear, endMonth, endDay;
if (matcher.group("stdEndY") != null) {
// 标准格式
endYear = Integer.parseInt(matcher.group("stdEndY"));
endMonth = Integer.parseInt(matcher.group("stdEndM"));
endDay = Integer.parseInt(matcher.group("stdEndD"));
} else {
// 中文格式
endYear = Integer.parseInt(matcher.group("cnEndY"));
endMonth = Integer.parseInt(matcher.group("cnEndM"));
endDay = Integer.parseInt(matcher.group("cnEndD"));
}
LocalDate startDate = LocalDate.of(startYear, startMonth, startDay);
LocalDate endDate = LocalDate.of(endYear, endMonth, endDay);
return Arrays.asList(Date.from(startDate.atStartOfDay(ZoneId.systemDefault()).toInstant()), Date.from(endDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
} catch (Exception e) {
log.error("", e);
return null; // 处理无效日期
}
}
return null;
}
public static void main(String[] args) {
String[] testCases = {
"2020-10-10到2020-10-11",
"2020年10月10日——2020年10月11日",
"2020/10/10~2020/10/11",
"2020 年 1 月 5 日 至 2020 年 1 月 6 日",
"2020.12.31—2021.01.01"
};
for (String testCase : testCases) {
List<Date> dates = parseDateRange(testCase);
if (dates != null) {
System.out.printf("输入: '%s'\n开始日期: %s\n结束日期: %s\n\n", testCase, dates.get(0), dates.get(1));
} else {
System.out.printf("输入: '%s' 解析失败\n\n", testCase);
}
}
}
2023-02-16 15:28:15 +08:00
public static String dealDateFormat(String oldDate) {
Date date1 = null;
SimpleDateFormat df2 = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date date = df.parse(oldDate);
2023-06-02 13:59:52 +08:00
Calendar beijingcal = Calendar.getInstance();
2023-02-16 15:28:15 +08:00
beijingcal.setTime(date);
beijingcal.set(Calendar.HOUR, beijingcal.get(Calendar.HOUR) + 8);
date1 = beijingcal.getTime();
df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return df2.format(date1);
}
public static String parseDateFormat(String oldDate) {
Date date = null;
SimpleDateFormat df2 = null;
try {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
date = df.parse(oldDate);
df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return df2.format(date);
}
2023-06-02 13:59:52 +08:00
public static List<String> getNowWeekAllDayList() {
2023-02-16 15:28:15 +08:00
Calendar cal = Calendar.getInstance();
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// log.info("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
2023-06-02 13:59:52 +08:00
return findDates(cal.getTime(), new Date());
}
2023-06-05 19:02:07 +08:00
2023-02-16 15:28:15 +08:00
/**
* 查询现在时间几天前到现在之间的所有日期
2023-06-02 13:59:52 +08:00
*
2023-02-16 15:28:15 +08:00
* @param days
* @return
*/
2023-06-02 13:59:52 +08:00
public static List<String> getOtherDayAllDayList(int days) {
2023-02-16 15:28:15 +08:00
Calendar cal = Calendar.getInstance();
2023-06-02 13:59:52 +08:00
cal.add(Calendar.DATE, -days);
return findDates(cal.getTime(), new Date());
2023-02-16 15:28:15 +08:00
}
2023-06-02 13:59:52 +08:00
public static List<String> getNowMonthAllDayList() {
2023-02-16 15:28:15 +08:00
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 1);
2023-06-02 13:59:52 +08:00
return findDates(cal.getTime(), new Date());
2023-02-16 15:28:15 +08:00
}
/**
2024-04-15 15:39:20 +08:00
* 获取两个时间段所有天数2024-01-01到2024-01-02...到2025-01-01
2023-06-02 13:59:52 +08:00
*
2024-04-15 15:39:20 +08:00
* @param beginTime 2024-01-01
* @param endTime 2025-01-01
2023-02-16 15:28:15 +08:00
* @return
*/
2023-06-02 13:59:52 +08:00
public static List<String> getDiffTimeDayList(String beginTime, String endTime) {
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
2023-06-02 13:59:52 +08:00
return findDates(sdf.parse(beginTime), sdf.parse(endTime));
2023-02-16 15:28:15 +08:00
} catch (ParseException e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return null;
}
2023-06-02 13:59:52 +08:00
public static List<String> findDates(Date dBegin, Date dEnd) {
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
List lDate = new ArrayList();
lDate.add(sdf.format(dBegin));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
2023-06-02 13:59:52 +08:00
while (dEnd.after(calBegin.getTime())) {
2023-02-16 15:28:15 +08:00
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.DAY_OF_MONTH, 1);
2023-06-02 13:59:52 +08:00
if (calBegin.getTimeInMillis() <= dEnd.getTime()) {
2023-02-16 15:28:15 +08:00
lDate.add(sdf.format(calBegin.getTime()));
}
}
return lDate;
}
/**
* 获取指定年份所有天数
2023-06-02 13:59:52 +08:00
*
2023-02-16 15:28:15 +08:00
* @param yearMonth
* @return
*/
public static List<String> getYearAllMonthList(String yearMonth) {
2023-06-02 13:59:52 +08:00
String year = yearMonth.substring(0, 4);
List<String> list = new ArrayList<>();
for (int i = 1; i <= 12; i++) {
if (i < 10) {
list.add(year + "0" + i);
} else {
list.add(year + i);
2023-02-16 15:28:15 +08:00
}
}
return list;
}
/**
2023-06-02 13:59:52 +08:00
* @param yearMonth 月份格式yyyy-MM
2023-02-16 15:28:15 +08:00
* @return
*/
public static List<String> getMonthDayList(String yearMonth) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//判断是否是当月
2023-06-02 13:59:52 +08:00
if (yearMonth.equals(sdf.format(new Date()))) {
2023-02-16 15:28:15 +08:00
return getNowMonthAllDayList();
2023-06-02 13:59:52 +08:00
} else {
2023-02-16 15:28:15 +08:00
return getDayListOfMonth(yearMonth);
}
}
2023-06-02 13:59:52 +08:00
2023-02-16 15:28:15 +08:00
public static List<String> getDayListOfMonth(String yearMonth) {
int year = Integer.parseInt(yearMonth.split("-")[0]); //年
int month = Integer.parseInt(yearMonth.split("-")[1]); //月
Calendar endCal = Calendar.getInstance();
// 设置年份
endCal.set(Calendar.YEAR, year);
// 设置月份
endCal.set(Calendar.MONTH, month - 1);
// 获取某月最大天数
int lastDay = endCal.getActualMaximum(Calendar.DATE);
// 设置日历中月份的最大天数
endCal.set(Calendar.DAY_OF_MONTH, lastDay);
Calendar startCal = Calendar.getInstance();
// 设置年份
startCal.set(Calendar.YEAR, year);
// 设置月份
startCal.set(Calendar.MONTH, month - 1);
//获取某月最小天数
int firstDay = startCal.getActualMinimum(Calendar.DAY_OF_MONTH);
//设置日历中月份的最小天数
startCal.set(Calendar.DAY_OF_MONTH, firstDay);
2023-06-02 13:59:52 +08:00
return findDates(startCal.getTime(), endCal.getTime());
2023-02-16 15:28:15 +08:00
}
/**
* 获取当前周第一天日期
2023-06-02 13:59:52 +08:00
*
2023-02-16 15:28:15 +08:00
* @return
*/
2023-06-02 13:59:52 +08:00
public static String getNowWeekStartTime() {
2023-02-16 15:28:15 +08:00
Calendar cal = Calendar.getInstance();
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
// 获得当前日期是一个星期的第几天
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// log.info("要计算日期为:" + sdf.format(cal.getTime())); // 输出要计算日期
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
return sdf.format(cal.getTime());
}
2023-06-02 13:59:52 +08:00
public static List<String> getDiffTimeYearList(String beginTime, String endTime) {
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
2023-06-02 13:59:52 +08:00
return findYear(sdf.parse(beginTime), sdf.parse(endTime));
2023-02-16 15:28:15 +08:00
} catch (ParseException e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return null;
}
2023-06-02 13:59:52 +08:00
public static List<String> findYear(Date dBegin, Date dEnd) {
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
List lDate = new ArrayList();
lDate.add(sdf.format(dBegin));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
2023-06-02 13:59:52 +08:00
while (dEnd.after(calBegin.getTime())) {
2023-02-16 15:28:15 +08:00
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.YEAR, 1);
2023-06-02 13:59:52 +08:00
if (calBegin.getTimeInMillis() <= dEnd.getTime()) {
2023-02-16 15:28:15 +08:00
lDate.add(sdf.format(calBegin.getTime()));
}
}
return lDate;
}
2023-06-02 13:59:52 +08:00
public static List<String> getDiffTimeMonthList(String beginTime, String endTime) {
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
2023-06-02 13:59:52 +08:00
return findMonth(sdf.parse(beginTime), sdf.parse(endTime));
2023-02-16 15:28:15 +08:00
} catch (ParseException e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return null;
}
2023-06-02 13:59:52 +08:00
public static List<String> findMonth(Date dBegin, Date dEnd) {
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
List lDate = new ArrayList();
lDate.add(sdf.format(dBegin));
Calendar calBegin = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calBegin.setTime(dBegin);
Calendar calEnd = Calendar.getInstance();
// 使用给定的 Date 设置此 Calendar 的时间
calEnd.setTime(dEnd);
// 测试此日期是否在指定日期之后
2023-06-02 13:59:52 +08:00
while (dEnd.after(calBegin.getTime())) {
2023-02-16 15:28:15 +08:00
// 根据日历的规则,为给定的日历字段添加或减去指定的时间量
calBegin.add(Calendar.MONTH, 1);
2023-06-02 13:59:52 +08:00
if (calBegin.getTimeInMillis() <= dEnd.getTime()) {
2023-02-16 15:28:15 +08:00
lDate.add(sdf.format(calBegin.getTime()));
}
}
return lDate;
}
2023-06-02 13:59:52 +08:00
public static List<String> getDiffTimeWeekList(String beginTime, String endTime) {
2023-02-16 15:28:15 +08:00
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
2023-06-02 13:59:52 +08:00
LocalDate startDate = LocalDate.parse(beginTime, formatter);
LocalDate endDate = LocalDate.parse(endTime, formatter);
2023-02-16 15:28:15 +08:00
List<String> lDate = new ArrayList();
2023-06-02 13:59:52 +08:00
while (endDate.isAfter(startDate)) {
String start = startDate.format(formatter);
if (startDate.getDayOfWeek() == DayOfWeek.SUNDAY) {
2023-02-16 15:28:15 +08:00
lDate.add(start + "/" + start);
2023-06-02 13:59:52 +08:00
} else {
2023-02-16 15:28:15 +08:00
LocalDate sunday = startDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY));
if (sunday.isAfter(endDate)) {
lDate.add(start + "/" + endDate.format(formatter));
} else {
lDate.add(start + "/" + sunday.format(formatter));
}
}
2023-06-02 13:59:52 +08:00
startDate = startDate.with(TemporalAdjusters.next(DayOfWeek.SUNDAY)).plusDays(1);
2023-02-16 15:28:15 +08:00
}
return lDate;
}
/**
* 获取当前时间和当天时间几天前之间的所有天数
2023-06-02 13:59:52 +08:00
*
* @param dayNum 当天时间以前具体几天
2023-02-16 15:28:15 +08:00
* @return
*/
2023-06-02 13:59:52 +08:00
public static List<String> getNowFewDaysAgoList(int dayNum) {
2023-02-16 15:28:15 +08:00
try {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DATE, -dayNum);
Date date = calendar.getTime();
2023-06-02 13:59:52 +08:00
return findDates(date, new Date());
2023-02-16 15:28:15 +08:00
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return null;
}
/**
* @param type 1 今日24 小时2本周所有天3本月所有天
* @return
*/
public static List<String> getDayList(Integer type) {
2023-06-02 13:59:52 +08:00
if (type == 1) {
List<String> list = new ArrayList<>();
for (int i = 0; i <= 23; i++) {
list.add(i + "");
2023-02-16 15:28:15 +08:00
}
return list;
2023-06-02 13:59:52 +08:00
} else if (type == 2) {
2024-01-04 18:16:34 +08:00
return getDateTimeStrList(60, "yyyy-MM-dd");
2023-06-02 13:59:52 +08:00
} else if (type == 3) {
2023-02-16 15:28:15 +08:00
return getNowMonthAllDayList();
}
return null;
}
/**
* 比较两个日期大小
2023-06-02 13:59:52 +08:00
*
2023-02-16 15:28:15 +08:00
* @param time1
* @param time2
* @return
* @throws ParseException
*/
2023-06-02 13:59:52 +08:00
public static boolean compareTime(String time1, String time2) {
2023-02-16 15:28:15 +08:00
try {
//如果想比较日期则写成"yyyy-MM-dd"就可以了
2023-06-02 13:59:52 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
2023-02-16 15:28:15 +08:00
//将字符串形式的时间转化为Date类型的时间
2023-06-02 13:59:52 +08:00
Date a = sdf.parse(time1);
Date b = sdf.parse(time2);
2023-02-16 15:28:15 +08:00
//Date类的一个方法如果a早于b返回true否则返回false
//a时间下雨b时间返回true,否正返回false
2023-06-02 13:59:52 +08:00
if (a.before(b)) {
2023-02-16 15:28:15 +08:00
return true;
2023-06-02 13:59:52 +08:00
} else {
2023-02-16 15:28:15 +08:00
return false;
}
2023-06-02 13:59:52 +08:00
} catch (Exception e) {
2024-04-14 21:05:01 +08:00
log.error("error", e);
2023-02-16 15:28:15 +08:00
}
return false;
}
2023-06-02 13:59:52 +08:00
2023-02-16 15:28:15 +08:00
/**
* 查询当前时间指定天数之前的日期
2023-06-02 13:59:52 +08:00
*
* @param @param days
* @param @return 参数
2023-02-16 15:28:15 +08:00
* @return String 返回类型
* @throws
2023-06-02 13:59:52 +08:00
* @Title: getBeginDayTime
2023-02-16 15:28:15 +08:00
*/
2023-06-02 13:59:52 +08:00
public static String getBeginDayTime(int days) {
2023-02-16 15:28:15 +08:00
Calendar cal = Calendar.getInstance();
2023-06-02 13:59:52 +08:00
cal.add(Calendar.DATE, -days);
2023-02-16 15:28:15 +08:00
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(cal.getTime());
}
2023-06-05 19:02:07 +08:00
2023-06-07 11:26:18 +08:00
/**
* 转成GMT时间
*
* @param date
*/
public static String getGMTTime(Date date) {
DateFormat dateFormat = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(date);
}
2023-07-24 18:29:04 +08:00
2023-09-15 14:51:12 +08:00
/**
* 查询N天前的所有天数2023-06-10到2023-06-15
*
* @return
*/
public static List<String> getDaysBefore(int days) {
ArrayList<String> rtList = new ArrayList<>();
DateTime dateTime = DateUtil.parseDate(DateUtil.today());
for (int i = 0; i < days; i++) {
rtList.add(DateUtil.formatDate(DateUtil.offsetDay(dateTime, i * (-1))));
}
Collections.reverse(rtList);
return rtList;
}
2023-07-31 15:20:41 +08:00
/**
* 查询指定天数内所有天数列表, 返回[07-30, 07-31]
*
* @param queryStartTime 2023-07-31
* @param queryEndTime 2023-07-31
* @return
*/
2023-09-15 14:51:12 +08:00
public static List<String> getBetweenDays(String queryStartTime, String queryEndTime, String format) {
2023-07-31 15:20:41 +08:00
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = DateUtil.parseDate(queryStartTime);
DateTime endDate = DateUtil.parseDate(queryEndTime);
2023-10-31 18:59:50 +08:00
long offset = DateUtil.betweenDay(beginDate, endDate, true);
2023-07-31 15:20:41 +08:00
for (int i = 0; i < offset + 1; i++) {
2023-10-31 18:59:50 +08:00
rtList.add(DateUtil.format(DateUtil.offsetDay(beginDate, i), format));
2023-07-31 15:20:41 +08:00
}
return rtList;
}
2023-10-31 17:45:22 +08:00
/**
2023-11-03 11:17:47 +08:00
* 偏移日期str传入2023-11-02返回2023-11-02传入2023-11-02 00:00:00返回2023-11-02 00:00:00
2023-10-31 17:45:22 +08:00
*
* @param dayStr
* @param offset
* @return
*/
public static String offsetDayStr(String dayStr, int offset) {
2023-11-03 11:17:47 +08:00
if (dayStr.length() == 10) {
return DateUtil.formatDate(DateUtil.offsetDay(DateUtil.parse(dayStr), offset));
} else {
return DateUtil.format(DateUtil.offsetDay(DateUtil.parse(dayStr), offset), "yyyy-MM-dd HH:mm:ss");
}
2023-10-31 17:45:22 +08:00
}
2023-10-31 18:59:50 +08:00
/**
2024-01-04 18:16:34 +08:00
* 获取所有日期时间列表
2023-10-31 18:59:50 +08:00
*
* @param type 类型
2023-12-04 10:13:15 +08:00
* 10.近12小时,20.近24小时每小时,30.近半天,40.近一天,50.近两天,60.近一周每天,70.近两周,72.近15天每天,80.近一个月每天,90.近半年每月,93.近一年每月
* 100.今天每小时,200.昨天,300.本周,400.本月每天,500.本年每月,600.去年
2023-10-31 18:59:50 +08:00
* @param pattern 格式 yyyy-MM-dd HH:mm:ss
* @return
*/
2024-01-04 18:16:34 +08:00
public static List<String> getDateTimeStrList(int type, String pattern) {
2023-12-04 10:13:15 +08:00
if (type == 20) {
//近24小时每小时egHH:00
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = DateUtil.yesterday();
DateTime endDate = DateTime.now();
long offset = DateUtil.between(beginDate, endDate, DateUnit.HOUR);
for (int i = 1; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetHour(beginDate, i), pattern));
}
return rtList;
} else if (type == 60) {
//近一周(每天)
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = DateUtil.lastWeek();
DateTime endDate = DateTime.now();
long offset = DateUtil.betweenDay(beginDate, endDate, true);
2024-03-16 11:58:15 +08:00
for (int i = 1; i < offset + 1; i++) {
2023-12-04 10:13:15 +08:00
rtList.add(DateUtil.format(DateUtil.offsetDay(beginDate, i), pattern));
}
return rtList;
} else if (type == 72) {
//近15天每天
ArrayList<String> rtList = new ArrayList<>();
DateTime dateTime = DateUtil.parseDate(DateUtil.today());
for (int i = 0; i < 15; i++) {
rtList.add(DateUtil.format(DateUtil.offsetDay(dateTime, i * (-1)), pattern));
}
Collections.reverse(rtList);
return rtList;
} else if (type == 80) {
//近一个月(每天)
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = DateUtil.lastMonth();
DateTime endDate = DateTime.now();
long offset = DateUtil.betweenDay(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetDay(beginDate, i), pattern));
}
return rtList;
2023-10-31 18:59:50 +08:00
} else if (type == 90) {
2023-12-04 10:13:15 +08:00
//近半年(每月)
2023-10-31 18:59:50 +08:00
ArrayList<String> rtList = new ArrayList<>();
DateTime endDate = DateTime.now();
DateTime beginDate = DateUtil.offsetMonth(endDate, -6);
long offset = DateUtil.betweenMonth(beginDate, endDate, true);
for (int i = 1; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetMonth(beginDate, i), pattern));
}
return rtList;
} else if (type == 93) {
2023-12-04 10:13:15 +08:00
//近一年(每月)
2023-10-31 18:59:50 +08:00
ArrayList<String> rtList = new ArrayList<>();
DateTime endDate = DateTime.now();
DateTime beginDate = DateUtil.offsetMonth(endDate, -12);
long offset = DateUtil.betweenMonth(beginDate, endDate, true);
for (int i = 1; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetMonth(beginDate, i), pattern));
}
return rtList;
2023-12-04 10:13:15 +08:00
} else if (type == 100) {
//100.今天(每小时)
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = DateUtil.beginOfDay(DateTime.now());
DateTime endDate = DateUtil.endOfDay(DateTime.now());
long offset = DateUtil.between(beginDate, endDate, DateUnit.HOUR);
for (int i = 0; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetHour(beginDate, i), pattern));
}
return rtList;
} else if (type == 400) {
//本月(每天)
ArrayList<String> rtList = new ArrayList<>();
Date now = new Date();
DateTime beginDate = DateUtil.beginOfMonth(now);
DateTime endDate = DateUtil.endOfMonth(now);
long offset = DateUtil.betweenDay(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetDay(beginDate, i), pattern));
}
return rtList;
2023-12-02 17:16:10 +08:00
} else if (type == 500) {
2023-12-04 10:13:15 +08:00
//本年(每月)
2023-12-02 17:16:10 +08:00
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = DateUtil.beginOfYear(DateTime.now());
DateTime endDate = DateUtil.endOfYear(DateTime.now());
long offset = DateUtil.betweenMonth(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(DateUtil.format(DateUtil.offsetMonth(beginDate, i), pattern));
}
return rtList;
2023-10-31 18:59:50 +08:00
}
2023-12-04 10:13:15 +08:00
return new ArrayList<>();
2023-10-31 18:59:50 +08:00
}
2023-12-18 14:55:59 +08:00
/**
* str转换成datetime格式
*
* @param str
* @return
*/
public static DateTime parse(String str) {
if (Objects.equals(str.length(), 7)) {
return DateUtil.parse(str, "yyyy-MM");
}
return DateUtil.parse(str);
}
2024-03-19 10:28:12 +08:00
/**
* 是否正确的日期格式1:yyyy-MM-dd
*
* @param timeStr
* @param type
* @return
*/
public static boolean isValidDateFormat(String timeStr, int type) {
if (type == 1) {
String datePattern = "\\d{4}-\\d{2}-\\d{2}";
// 编译正则表达式模式
Pattern pattern = Pattern.compile(datePattern);
// 创建 Matcher 对象
Matcher matcher = pattern.matcher(timeStr);
// 检查是否匹配
if (matcher.matches()) {
return true;
} else {
return false;
}
}
return false;
}
2024-04-10 13:36:06 +08:00
/**
* 获取ISO8601时间秒级如2024-04-10T09:57:41+08:00
*
* @return
*/
public static String getISO8601Str(Date date) {
String pattern = "YYYY-MM-dd'T'HH:mm:ssZZ";
return DateFormatUtils.format(date, pattern);
}
2024-05-16 17:13:05 +08:00
/**
* 获取ISO8601时间秒级如2017-06-14T00:00:00.000+08:00
*
* @return
*/
public static String getISO8601StrWithMs(Date date) {
String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
return DateFormatUtils.format(date, pattern);
}
2024-04-10 13:36:06 +08:00
/**
* 判断时间格式 格式必须为yyyy-MM-dd HH:mm:ss
*
* @param sDate
* @return
*/
public static void checkLegalDate19(String sDate) {
int legalLen = 19;
if ((sDate == null) || (sDate.length() != legalLen)) {
throw new OpenAlertException("时间格式格式必须为yyyy-MM-dd HH:mm:ss");
}
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = formatter.parse(sDate);
boolean equals = sDate.equals(formatter.format(date));
if (!equals) {
throw new OpenAlertException("时间格式格式必须为yyyy-MM-dd HH:mm:ss");
}
} catch (Exception e) {
throw new OpenAlertException("时间格式格式必须为yyyy-MM-dd HH:mm:ss");
}
}
2024-05-16 17:13:05 +08:00
public static Date convertQueryDate(String dateStr, boolean isBegin) {
Date date = DateUtil.parse(dateStr);
if (dateStr.length() == 10) {
if (isBegin) {
date = DateUtil.beginOfDay(date);
} else {
date = DateUtil.endOfDay(date);
}
}
return date;
}
2024-07-23 22:18:30 +08:00
/**
* 合并去重时间段
*
* @return
*/
public static List<Map<String, Date>> getTimePeriodListDumplictcatePeriod(List<Map<String, Date>> periodList) {
List<Map<String, Date>> result = new ArrayList<>();
//列表不能为空
if (periodList == null) {
return null;
}
//对数据排序,开始时间从小到大
Collections.sort(periodList, new Comparator<Map<String, Date>>() {
@Override
public int compare(Map<String, Date> u1, Map<String, Date> u2) {
long diff = u1.get("startDate").getTime() - u2.get("startDate").getTime();
if (diff > 0) {
return 1;
} else if (diff < 0) {
return -1;
}
return 0; //相等为0
}
});
for (int i = 0; i < periodList.size() - 1; i++) {
int j = i + 1;
//判断 i的Endtime 与j 的Begintime 是否有交叉,有交叉,更新 j的Begintime 为i的Begintime, 并移除i
if (periodList.get(i).get("endDate").after(periodList.get(j).get("startDate"))) {
periodList.get(j).put("startDate", periodList.get(i).get("startDate"));
periodList.remove(i);
i--;
}
}
result = periodList;
return result;
}
2024-12-02 18:09:41 +08:00
/**
2024-12-05 16:17:42 +08:00
* 获取下一个周几不包含自己
2024-12-02 18:09:41 +08:00
*
* @param d
* @param i 1对应周一7对应周日
* @return
*/
public static Date getNextDayOfWeek(Date d, int i) {
2024-12-05 16:17:42 +08:00
LocalDate localDate = LocalDate.parse(DateUtil.formatDate(d)).with(DayOfWeek.of(i));
2024-12-02 18:09:41 +08:00
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
2024-12-05 16:17:42 +08:00
Date from = Date.from(zonedDateTime.toInstant());
if (DateUtil.compare(DateUtil.parseDate(DateUtil.formatDate(d)), from) >= 0) {
from = DateUtil.offsetDay(from, 7);
} else {
from = DateUtil.offsetDay(from, 0);
}
return from;
}
/**
* 获取上一个周几包含自己
*
* @param d
* @param i 1对应周一7对应周日
* @return
*/
public static Date getBeforeDayOfWeek(Date d, int i) {
LocalDate localDate = LocalDate.parse(DateUtil.formatDate(d)).with(DayOfWeek.of(i));
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
Date from = Date.from(zonedDateTime.toInstant());
if (DateUtil.compare(DateUtil.parseDate(DateUtil.formatDate(d)), from) < 0) {
from = DateUtil.offsetDay(from, -7);
} else {
from = DateUtil.offsetDay(from, 0);
}
return from;
2024-12-02 18:09:41 +08:00
}
2024-07-23 22:18:30 +08:00
2023-02-16 15:28:15 +08:00
}