2024-07-08 17:41:17 +08:00
|
|
|
package com.gunshi.project.xyt.util;
|
|
|
|
|
|
|
|
|
|
import java.text.ParseException;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Description:
|
|
|
|
|
* Created by wanyan on 2024/2/28
|
|
|
|
|
*
|
|
|
|
|
* @author wanyan
|
|
|
|
|
* @version 1.0
|
|
|
|
|
*/
|
|
|
|
|
public class DateUtil {
|
|
|
|
|
|
|
|
|
|
private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
|
|
|
|
|
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
|
|
private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd日HH时");
|
|
|
|
|
|
|
|
|
|
private static DateTimeFormatter ym = DateTimeFormatter.ofPattern("YYYYMM");
|
|
|
|
|
|
|
|
|
|
public static Date convertStringToDate(String str){
|
|
|
|
|
try {
|
|
|
|
|
return df.parse(str);
|
|
|
|
|
} catch (ParseException e) {
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-10 14:04:43 +08:00
|
|
|
public static final ThreadLocal<SimpleDateFormat> sdfhmsS = new ThreadLocal<SimpleDateFormat>() {
|
|
|
|
|
protected SimpleDateFormat initialValue() {
|
|
|
|
|
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-08 17:41:17 +08:00
|
|
|
public static String convertDateToString(Date date){
|
|
|
|
|
return sdf.format(date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String convertDateToMDSString(Date date){
|
|
|
|
|
return df.format(date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 年初
|
|
|
|
|
* @param year
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static LocalDateTime beginningOfYear(Integer year){
|
|
|
|
|
return LocalDateTime.of(year, 1, 1, 0, 0, 0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 年末
|
|
|
|
|
* @param year
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
public static LocalDateTime endOfYear(Integer year){
|
|
|
|
|
return beginningOfYear(year).plusYears(1).minusSeconds(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将Date对象转换为LocalDateTime对象.
|
|
|
|
|
*
|
|
|
|
|
* @param date 需要转换的Date对象
|
|
|
|
|
* @return 转换后的LocalDateTime对象
|
|
|
|
|
*/
|
|
|
|
|
public static Optional<LocalDateTime> convertFromDate(Date date) {
|
|
|
|
|
if (date == null) {
|
|
|
|
|
throw new IllegalArgumentException("date is null");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Instant instant = date.toInstant();
|
|
|
|
|
return Optional.of(instant.atZone(ZoneId.systemDefault()).toLocalDateTime());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String getIntegerTime(){
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
LocalDateTime roundedTime = now.withMinute(0).withSecond(0).withNano(0);
|
|
|
|
|
return roundedTime.format(formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getMinusIntegerTime(){
|
|
|
|
|
LocalDateTime yesterday = LocalDateTime.now().minusDays(1);
|
|
|
|
|
LocalDateTime roundedTime = yesterday.withMinute(0).withSecond(0).withNano(0);
|
|
|
|
|
return roundedTime.format(formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String convertDH(String str){
|
|
|
|
|
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
|
|
|
|
|
return dateTime.format(dtf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getMinusTime(String str,long hour){
|
|
|
|
|
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
|
|
|
|
|
LocalDateTime minusTime = dateTime.minusHours(hour);
|
|
|
|
|
return minusTime.format(formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getPlusTime(String str,long hour){
|
|
|
|
|
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
|
|
|
|
|
LocalDateTime minusTime = dateTime.plusHours(hour);
|
|
|
|
|
return minusTime.format(formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getTodayEight(){
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
LocalDateTime roundedTime = now.withHour(8).withMinute(0).withSecond(0).withNano(0);
|
|
|
|
|
return roundedTime.format(formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Integer getMD(int year, int month) {
|
|
|
|
|
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
|
|
|
|
|
return 31;
|
|
|
|
|
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
|
|
|
|
|
return 30;
|
|
|
|
|
} else if (month == 2) {
|
|
|
|
|
//判断是否是闰年
|
|
|
|
|
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
|
|
|
|
|
return 29;
|
|
|
|
|
} else {
|
|
|
|
|
return 28;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 31;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String getYM() {
|
|
|
|
|
LocalDateTime now = LocalDateTime.now();
|
|
|
|
|
return now.format(ym);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|