🍀JavaSE:时间转换工具类实现
一、Date 转 LocalDateTime
/**
* Date转LocalDateTime
*
* @param date Date
* @return LocalDateTime
*/
public static LocalDateTime date2LocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDateTime();
}
二、LocalDateTime 转 Date
/**
* LocalDateTime转Date
*
* @param localDateTime LocalDateTime
* @return Date
*/
public static Date localDateTime2Date(LocalDateTime localDateTime) {
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
三、Date 转 String
/**
* 字符串转日期
*
* @param date 日期字符串
* @param pattern 日期格式
* @return 日期
* @throws ParseException 解析异常
*/
public static String formatDateToString(Date date, String pattern) throws ParseException {
LocalDateTime localDateTime = date2LocalDateTime(date);
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return localDateTime.format(formatter);
} catch (DateTimeParseException e) {
log.error("日期转换异常", e);
throw new ParseException("无法识别的时间格式", 0);
}
}
四、String 转 Date
/**
* 字符串转日期
*
* @param text 日期字符串
* @param pattern 日期格式
* @return 日期
* @throws ParseException 解析异常
*/
public static Date formatStringToDate(String text, String pattern) throws ParseException {
LocalDateTime localDateTime = stringToLocalDateTime(text, pattern);
return localDateTimeToDate(localDateTime);
}
五、任意格式 String 转时间戳
private static final Integer MILLISECONDS_LENGTH = 13;
private static final Integer SECONDS_LENGTH = 10;
private static final String[] PATTERNS = {
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd",
"yyyy/MM/dd HH:mm:ss",
"yyyy/MM/dd",
"yyyyMMddHHmmss",
"yyyyMMdd"
};
/**
* 字符串转日期
* @param text 日期字符串,支持多种格式
* @return 日期
* @throws ParseException 解析异常
*/
public static Long strToTimes(String text) throws ParseException {
if (StringUtils.isBlank(text)) {
throw new ParseException("输入的时间字符串为空", 0);
}
text = text.trim();
// 如果是数字格式,判断是否为时间戳
if (StringUtils.isNumeric(text)) {
if (text.length() == MILLISECONDS_LENGTH) {
return Long.parseLong(text); // 毫秒时间戳
} else if (text.length() == SECONDS_LENGTH) {
return Long.parseLong(text) * 1000; // 秒时间戳转换为毫秒
} else {
throw new ParseException("无法识别的时间格式: " + text, 0);
}
}
// 尝试按照日期格式解析
for (String pattern : PATTERNS) {
Date date = null;
try {
date = formatStringToDate(text, pattern);
return date.getTime();
} catch (ParseException ignored) {
// 忽略异常,继续尝试下一个格式
}
}
throw new ParseException("无法识别的时间格式: " + text, 0);
}
完整代码:
@Slf4j
public class DateUtils {
/**
* no instance
*/
private DateUtils() {
}
private static final Integer MILLISECONDS_LENGTH = 13;
private static final Integer SECONDS_LENGTH = 10;
private static final String[] PATTERNS = {
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd",
"yyyy/MM/dd HH:mm:ss",
"yyyy/MM/dd",
"yyyyMMddHHmmss",
"yyyyMMdd"
};
/**
* Date转LocalDateTime
*
* @param date Date
* @return LocalDateTime
*/
public static LocalDateTime dateToLocalDateTime(Date date) {
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
return instant.atZone(zoneId).toLocalDateTime();
}
/**
* LocalDateTime转Date
*
* @param localDateTime LocalDateTime
* @return Date
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
return Date.from(zdt.toInstant());
}
/**
* 字符串转日期
*
* @param date 日期字符串
* @param pattern 日期格式
* @return 日期
* @throws ParseException 解析异常
*/
public static String formatDateToString(Date date, String pattern) throws ParseException {
LocalDateTime localDateTime = dateToLocalDateTime(date);
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return localDateTime.format(formatter);
} catch (DateTimeParseException e) {
log.error("日期转换异常", e);
throw new ParseException("无法识别的时间格式", 0);
}
}
/**
* 字符串转日期
*
* @param text 日期字符串
* @param pattern 日期格式
* @return 日期
* @throws ParseException 解析异常
*/
public static LocalDateTime stringToLocalDateTime(String text, String pattern) throws ParseException {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(text, formatter);
} catch (DateTimeParseException e) {
log.error("类型转换异常", e);
throw new ParseException("无法识别的时间格式", 0);
}
}
/**
* 字符串转日期
*
* @param text 日期字符串
* @param pattern 日期格式
* @return 日期
* @throws ParseException 解析异常
*/
public static Date formatStringToDate(String text, String pattern) throws ParseException {
LocalDateTime localDateTime = stringToLocalDateTime(text, pattern);
return localDateTimeToDate(localDateTime);
}
/**
* 字符串转日期
* @param text 日期字符串,支持多种格式
* @return 日期
* @throws ParseException 解析异常
*/
public static Long strToTimes(String text) throws ParseException {
if (StringUtils.isBlank(text)) {
throw new ParseException("输入的时间字符串为空", 0);
}
text = text.trim();
// 如果是数字格式,判断是否为时间戳
if (StringUtils.isNumeric(text)) {
if (text.length() == MILLISECONDS_LENGTH) {
return Long.parseLong(text); // 毫秒时间戳
} else if (text.length() == SECONDS_LENGTH) {
return Long.parseLong(text) * 1000; // 秒时间戳转换为毫秒
} else {
throw new ParseException("无法识别的时间格式: " + text, 0);
}
}
// 尝试按照日期格式解析
for (String pattern : PATTERNS) {
Date date;
try {
date = formatStringToDate(text, pattern);
return date.getTime();
} catch (ParseException ignored) {
// 忽略异常,继续尝试下一个格式
}
}
throw new ParseException("无法识别的时间格式: " + text, 0);
}
}
License:
CC BY 4.0