前两天用到日期操作,发现程序数据错乱.经大佬排查后最终确定问题出在simpleDateFormat 和 Date,因为线程非安全导致数据串了.研究后发现java8提供的新的日期处理工具是线程安全的,遂改为java8的日期,问题解决.现根据项目中用的整理了下常用的一些操作:
/**
* @description: java8 日期工具类
* @author: zhousc
* @create: 2021-07-15
**/
public class DateTimeUtil {
public enum DateTimeEnum {
BEFORE, AFTER, YEAR, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, NANO,
}
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss:SSS";
/**
* 时区
*/
public static ZoneId zoneId = ZoneId.systemDefault();
/**
* 日期格式化
*
* @param format
* @return
*/
public static DateTimeFormatter getFormatter(String format) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM_SS);
try {
if (StringUtils.isNotBlank(format)) {
dtf = DateTimeFormatter.ofPattern(format);
}
} catch (Exception e) {
e.printStackTrace();
}
return dtf;
}
/**
* 获取当前时间
*
* @return
*/
public static LocalDateTime getLocalDateTime() {
return LocalDateTime.now();
}
/**
* 获取指定格式时间
*
* @param format
* @return
*/
public static String getTimeByFormat(String format, LocalDateTime dateTime) {
String nowTime = null;
DateTimeFormatter formatter = getFormatter(format);
try {
if (dateTime == null) {
dateTime = getLocalDateTime();
}
nowTime = dateTime.format(formatter);
} catch (Exception e) {
e.printStackTrace();
}
return nowTime;
}
/**
* 根据日期格式获指定日期取毫秒值, 不穿日期默认为当前日期
*
* @param date
* @param format
* @return
*/
public static long getDateStrToMills(String date, String format) {
long mills = 0l;
DateTimeFormatter formatter = getFormatter(format);
try {
if (StringUtils.isNotBlank(date)) {
mills = LocalDateTime.parse(date, formatter).toInstant(ZoneOffset.of("+8")).toEpochMilli();
} else {
mills = Instant.now().toEpochMilli();
}
} catch (Exception e) {
e.printStackTrace();
}
return mills;
}
/**
* date 转 localDateTime
*
* @param date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
return date.toInstant().atZone(zoneId).toLocalDateTime();
}
/**
* localDateTime 转 Date
*
* @param localDateTime
* @return
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
return Date.from(localDateTime.atZone(zoneId).toInstant());
}
/**
* 字符串转localDateTime
*
* @param date
* @param format
* @return
*/
public static LocalDateTime dateStrToLocalDateTime(String date, String format) {
DateTimeFormatter formatter = getFormatter(format);
LocalDateTime ldt = LocalDateTime.parse(date, formatter);
return ldt;
}
/**
* 将Long类型的时间戳转换成String 类型的时间格式,时间格式为:yyyy-MM-dd HH:mm:ss
*/
public static String timeToString(long time, String format) {
Assert.notNull(time, "time is null");
DateTimeFormatter dtf = getFormatter(format);
return dtf.format(LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.systemDefault()));
}
/**
* 获取当前月份的第N天的时间ZoneDateTime
* @param zoneId
* @param dayNo
* @return
*/
public static ZonedDateTime getNoDayOfMonth(ZoneId zoneId,int dayNo) {
return ZonedDateTime.now(zoneId).withDayOfMonth(dayNo);
}
/**
* 获取当前月份的第N天的时间ZoneDateTime
* @param zoneId
* @param dayNo
* @return
*/
public static ZonedDateTime getNoDayOfYear(ZoneId zoneId,int dayNo) {
return ZonedDateTime.now(zoneId).withDayOfYear(dayNo);
}
/**
* 将ZoneDateTime 转成 指定时区偏移量的LocalDateTime
*
* @param zonedDateTime 时间
* @param zoneId 指定时区偏移量
* @return
*/
public static LocalDateTime zoneDateTimeToLocalDateTime(ZonedDateTime zonedDateTime, ZoneId zoneId) {
return zonedDateTime.toInstant().atZone(zoneId).toLocalDateTime();
}
/**
* @param date 基础时间,默认当前时间
* @param setType 偏移类型
* @param dateType 日期类型,默认毫秒
* @param setNum 偏移时间
* @param format
* @return
*/
public static String getOffsetDateByDate(LocalDateTime date, DateTimeEnum setType, DateTimeEnum dateType, int setNum, String format) {
String dateTime = null;
DateTimeFormatter formatter = getFormatter(format);
try {
if (setType.equals(DateTimeEnum.AFTER)) {
setNum = -setNum;
}
LocalDateTime timePlusBySetNum = getTimePlusBySetNum(date, dateType, setNum);
dateTime = timePlusBySetNum.format(formatter);
} catch (Exception e) {
e.printStackTrace();
}
return dateTime;
}
/**
* 日期运算后日期
* @param date
* @param dateType
* @param setNum
* @return
*/
public static LocalDateTime getTimePlusBySetNum(LocalDateTime date,DateTimeEnum dateType,int setNum) {
LocalDateTime dateTime = null;
try {
if (DateTimeEnum.YEAR.equals(dateType)) {
dateTime = date.plusYears(setNum);
} else if (DateTimeEnum.MONTH.equals(dateType)) {
dateTime = date.plusMonths(setNum);
} else if (DateTimeEnum.DAY.equals(dateType)) {
dateTime = date.plusDays(setNum);
} else if (DateTimeEnum.HOUR.equals(dateType)) {
dateTime = date.plusHours(setNum);
} else if (DateTimeEnum.MINUTE.equals(dateType)) {
dateTime = date.plusMinutes(setNum);
} else if (DateTimeEnum.SECOND.equals(dateType)) {
dateTime = date.plusSeconds(setNum);
} else if (DateTimeEnum.WEEK.equals(dateType)) {
dateTime = date.plusWeeks(setNum);
} else {
dateTime = date.plusNanos(setNum);
}
} catch (Exception e) {
e.printStackTrace();
}
return dateTime;
}
/**
* 获取两个日期之间 间隔setNum 所有时间
*
* @param dateFirst
* @param dateSecond
* @param dateType
* @param setNum
* @param format
* @return
*/
public static List<String> getBetweenDates(String dateFirst, String dateSecond, DateTimeEnum dateType, int setNum, String format) {
List<String> dates = new ArrayList<>();
DateTimeFormatter dtf = getFormatter(format);
LocalDateTime startDate = LocalDateTime.parse(dateFirst, dtf);
LocalDateTime endDate = LocalDateTime.parse(dateSecond, dtf);
try {
if (ObjectUtils.equals(startDate, endDate)) {
dates.add(startDate.format(dtf));
}
long between = getChronoUnit(dateType, startDate, endDate);
if (between >= 1) {
String offsetDateByDate = getOffsetDateByDate(startDate, DateTimeEnum.BEFORE, dateType, setNum, format);
LocalDateTime endDate2 = dateStrToLocalDateTime(offsetDateByDate, format);
while (getChronoUnit(dateType, endDate2, endDate) <= between && getChronoUnit(dateType, endDate2, endDate) >= 0) {
dates.add(endDate2.format(dtf));
endDate2 = getTimePlusBySetNum(endDate2, dateType, setNum);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return dates;
}
/**
* 两个 日期间隔时间
*
* @param dateType
* @param startDate
* @param endDate
* @return
*/
public static long getChronoUnit(DateTimeEnum dateType, LocalDateTime startDate, LocalDateTime endDate) {
long l = 0l;
try {
if (DateTimeEnum.YEAR.equals(dateType)) {
l = ChronoUnit.YEARS.between(startDate, endDate);
} else if (DateTimeEnum.MONTH.equals(dateType)) {
l = ChronoUnit.MONTHS.between(startDate, endDate);
} else if (DateTimeEnum.DAY.equals(dateType)) {
l = ChronoUnit.DAYS.between(startDate, endDate);
} else if (DateTimeEnum.WEEK.equals(dateType)) {
l = ChronoUnit.WEEKS.between(startDate, endDate);
} else if (DateTimeEnum.HOUR.equals(dateType)) {
l = ChronoUnit.HOURS.between(startDate, endDate);
} else if (DateTimeEnum.MINUTE.equals(dateType)) {
l = ChronoUnit.MINUTES.between(startDate, endDate);
} else if (DateTimeEnum.SECOND.equals(dateType)) {
l = ChronoUnit.SECONDS.between(startDate, endDate);
}
} catch (Exception e) {
e.printStackTrace();
}
return l;
}
// public static void main(String[] args) {
// String nowTimeByFormat = getTimeByFormat(YYYY_MM_DD_HH_MM_SS_SSS, null);
// System.out.println(" ====== " + nowTimeByFormat);
// long dateStrToMills = getDateStrToMills(nowTimeByFormat, YYYY_MM_DD_HH_MM_SS_SSS);
// System.out.println(" ====== " + dateStrToMills);
// LocalDateTime localDateTime = dateStrToLocalDateTime(nowTimeByFormat, YYYY_MM_DD_HH_MM_SS_SSS);
// String offsetDateByDate = getOffsetDateByDate(localDateTime, DateTimeEnum.BEFORE, null, 10, YYYY_MM_DD_HH_MM_SS_SSS);
// System.out.println(" ====== " + offsetDateByDate);
// List<String> betweenDates = getBetweenDates("2021-07-01 00:00:00", "2021-07-02 00:00:00", DateTimeEnum.HOUR, 2, YYYY_MM_DD_HH_MM_SS);
// for (String betweenDate : betweenDates) {
// System.out.println(betweenDate);
// }
//
// ZonedDateTime noDayOfMonth = getNoDayOfMonth(zoneId, 1);
// ZonedDateTime noDayOfYear = getNoDayOfYear(zoneId, 1);
// LocalDateTime localDateTime1 = zoneDateTimeToLocalDateTime(noDayOfMonth, zoneId);
// LocalDateTime localDateTime2 = zoneDateTimeToLocalDateTime(noDayOfYear, zoneId);
// String timeByFormat = getTimeByFormat(YYYY_MM_DD_HH_MM_SS, localDateTime1);
// String timeByFormat2 = getTimeByFormat(YYYY_MM_DD_HH_MM_SS, localDateTime2);
// System.out.println("当月第一天 ===== " + timeByFormat);
// System.out.println("当年第一天 ===== " + timeByFormat2);
//
//
// }
}
此util类会在开发过程中不断完善更新,欢迎各位大佬指正交流.