492 lines
18 KiB
Java
Raw Normal View History

2023-02-16 15:28:15 +08:00
package com.zhgd.xmgl.util;
2023-06-02 13:59:52 +08:00
import cn.hutool.core.date.DateTime;
2023-02-16 15:28:15 +08:00
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
2023-06-05 19:02:07 +08:00
import java.util.*;
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
**/
2023-06-05 19:02:07 +08:00
public class DateUtil extends cn.hutool.core.date.DateUtil {
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 oldDate
* @return
*/
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);
//SimpleDateFormat df1 = new SimpleDateFormat ("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
//date1 = df1.parse(date.toString());
beijingcal.set(Calendar.HOUR, beijingcal.get(Calendar.HOUR) + 8);
date1 = beijingcal.getTime();
df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
} catch (ParseException e) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
return df2.format(date);
}
public static String switchCreateTime(String createTime) {
String formatStr2 = null;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");//注意格式化的表达式
try {
2023-06-02 13:59:52 +08:00
Date time = format.parse(createTime);
2023-02-16 15:28:15 +08:00
String date = time.toString();
//将西方形式的日期字符串转换成java.util.Date对象
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", java.util.Locale.US);
Date datetime = (Date) sdf.parse(date);
//再转换成自己想要显示的格式
formatStr2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(datetime);
} catch (ParseException e) {
e.printStackTrace();
}
return formatStr2;
}
/**
* 查询本周所有天数
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 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());
}
/**
* 查询本月所有天数Fix2023-06-01到2023-06-30
*
* @return
*/
public static List<String> getNowMonthAllDayListFix() {
ArrayList<String> rtList = new ArrayList<>();
Date now = new Date();
DateTime beginDate = cn.hutool.core.date.DateUtil.beginOfMonth(now);
DateTime endDate = cn.hutool.core.date.DateUtil.endOfMonth(now);
long offset = cn.hutool.core.date.DateUtil.betweenDay(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(cn.hutool.core.date.DateUtil.formatDate(cn.hutool.core.date.DateUtil.offsetDay(beginDate, i)));
}
return rtList;
}
/**
* 查询一个月前内所有天数2023-06-10到2023-07-10
*
* @return
*/
public static List<String> getDaysBetweenLastMonth() {
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = cn.hutool.core.date.DateUtil.lastMonth();
DateTime endDate = DateTime.now();
long offset = cn.hutool.core.date.DateUtil.betweenDay(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(cn.hutool.core.date.DateUtil.formatDate(cn.hutool.core.date.DateUtil.offsetDay(beginDate, i)));
}
return rtList;
}
/**
2023-06-05 19:02:07 +08:00
* 查询一个周前内所有天数2023-06-10到2023-06-17
2023-06-02 13:59:52 +08:00
*
* @return
*/
public static List<String> getDaysBetweenLastWeek() {
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = cn.hutool.core.date.DateUtil.lastWeek();
DateTime endDate = DateTime.now();
long offset = cn.hutool.core.date.DateUtil.betweenDay(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(cn.hutool.core.date.DateUtil.formatDate(cn.hutool.core.date.DateUtil.offsetDay(beginDate, i)));
}
return rtList;
2023-02-16 15:28:15 +08:00
}
2023-06-05 19:02:07 +08:00
/**
* 查询一个周前内所有天数06-10到06-17
*
* @return
*/
public static List<String> getDaysBetweenLastWeekWithoutYear() {
ArrayList<String> rtList = new ArrayList<>();
DateTime beginDate = cn.hutool.core.date.DateUtil.lastWeek();
DateTime endDate = DateTime.now();
long offset = cn.hutool.core.date.DateUtil.betweenDay(beginDate, endDate, true);
for (int i = 0; i < offset + 1; i++) {
rtList.add(cn.hutool.core.date.DateUtil.format(cn.hutool.core.date.DateUtil.offsetDay(beginDate, i), "MM-dd"));
}
return rtList;
}
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
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 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
}
/**
* 获取两个时间段所有天数
2023-06-02 13:59:52 +08:00
*
2023-02-16 15:28:15 +08:00
* @param beginTime
* @param endTime
* @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) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
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) {
e.printStackTrace();
}
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) {
return getDaysBetweenLastWeek();
} 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) {
2023-02-16 15:28:15 +08:00
e.printStackTrace();
}
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
/**
* 获取一天内的小时列表
*
* @return
*/
public static List<String> getAllHourInDay() {
return Arrays.asList("00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00");
}
2023-02-16 15:28:15 +08:00
public static void main(String[] args) {
//log.info(dealDateFormat("2021-08-25T04:22:20+0000"));
System.out.println(getOtherDayAllDayList(10));
}
}