package com.xxx.xxx.common.utils;
import com.xxx.xxx.common.enums.ResultCodeEnum;
import com.xxx.xxx.common.exception.InterceptorException;
import org.apache.commons.lang.StringUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
public final class DateUtil {
private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
private static DateTimeFormatter dateTimeFormatterDeatil = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
private static String rex = "\\d{4}-\\d{2}-\\d{2}";
public static ZonedDateTime date2ZoneDateTime(Date date){
return date == null ? null : ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
}
public static Date string2Date(String StringDate) {
if(StringUtils.isEmpty(StringDate)){
throw new InterceptorException(ResultCodeEnum.STRING_IS_NULL.code(), ResultCodeEnum.STRING_IS_NULL.message());
}
if(!StringDate.matches(rex)){
throw new InterceptorException(ResultCodeEnum.DATE_FORMAT_NOT_TRUE.code(), ResultCodeEnum.DATE_FORMAT_NOT_TRUE.message());
}
Date date = null;
try {
date = simpleDateFormat.parse(StringDate);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
public static ZonedDateTime string2ZonedDateTime(String StringDate) {
if (StringUtils.isEmpty(StringDate)) {
return null;
}
if (!StringDate.matches(rex)) {
throw new InterceptorException(ResultCodeEnum.DATE_FORMAT_NOT_TRUE.code(), ResultCodeEnum.DATE_FORMAT_NOT_TRUE.message());
}
ZonedDateTime zonedDateTime = null;
try {
zonedDateTime = LocalDate.parse(StringDate, dateTimeFormatter).atStartOfDay(ZoneId.systemDefault());
} catch (Exception e) {
e.printStackTrace();
}
return zonedDateTime;
}
public static ZonedDateTime string2ZonedDateTimeAddOne(String date){
if (StringUtils.isEmpty(date)) {
return null;
}
Calendar c = Calendar.getInstance();
c.setTime(DateUtil.string2Date(date));
c.add(Calendar.DAY_OF_MONTH, 1);
Date time = c.getTime();
ZonedDateTime dateTo = DateUtil.date2ZoneDateTime(time);
return dateTo;
}
public static ZonedDateTime getDayEndDate(String date) {
return LocalDateTime.of(LocalDate.parse(date, dateTimeFormatter), LocalTime.MAX)
.atZone(ZoneId.systemDefault());
}
public static String zonedDateTimeToDateString(ZonedDateTime dateTime) {
return zonedDateTimeToString(dateTime, dateTimeFormatter);
}
public static String zonedDateTimeToDateTimeString(ZonedDateTime dateTime) {
return zonedDateTimeToString(dateTime, dateTimeFormatterDeatil);
}
public static String zonedDateTimeToString(ZonedDateTime dateTime, DateTimeFormatter formatter) {
if (dateTime == null) {
return null;
}
String str = null;
try {
str = dateTime.format(formatter);
} catch (Exception e) {
e.printStackTrace();
return str;
}
return str;
}
public static ZonedDateTime getCurrentDayStartDate() {
return LocalDate.now().atStartOfDay(ZoneId.systemDefault());
}
public static ZonedDateTime truncDate(ZonedDateTime zonedDateTime) {
return null == zonedDateTime ? null : zonedDateTime.truncatedTo(ChronoUnit.DAYS);
}
public static ZonedDateTime getStartDateByUTC(ZonedDateTime zonedDateTime) {
return null == zonedDateTime ? null : zonedDateTime.toLocalDate().atStartOfDay(ZoneId.of("UTC"));
}
}