gunshi-project-ss/src/main/java/com/gunshi/project/xyt/util/DateUtil.java

158 lines
4.7 KiB
Java
Raw Normal View History

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;
2024-07-10 16:28:20 +08:00
import java.util.*;
2024-07-08 17:41:17 +08:00
/**
* 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);
}
}
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);
}
/**
* DateLocalDateTime.
*
* @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);
}
2024-07-10 16:28:20 +08:00
public static List<String> getDatesBetween(Date startDate, Date endDate) {
List<String> dates = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.setTime(startDate);
while (calendar.getTime().before(endDate)) {
dates.add(convertDateToString(calendar.getTime()));
calendar.add(Calendar.DATE, 1);
}
return dates.reversed();
}
2024-07-08 17:41:17 +08:00
}