简单说明
基于jdk1.8版本编写、测试;使用jdk1.8新日期、时间API;
支持情况
LocalDateTime -> String 与 String -> LocalDateTime 的互转String -> Long 与 Long -> String 的互转LocalDateTime -> Long 与 Long -> LocalDateTime 的互转基于LocalDateTime的日期类型比较(isAfter/isBefore/isEqual)
具体代码
import java.time.*;
import java.time.format.DateTimeFormatter;
public class DateTimeUtil {
public static String DATE_TIME_EN = "yyyy-MM-dd HH:mm:ss";
public static String DATE_TIME_ZH = "yyyy年MM月dd日 HH时mm分ss秒";
public static String DATE_EN = "yyyy-MM-dd";
public static String DATE_ZH = "yyyy年MM月dd日";
public static String TIME_EN = "HH:mm:ss";
public static String TIME_ZH = "HH时mm分ss秒";
public static DateTimeFormatter defaultFormatter;
static {
defaultFormatter = DateTimeFormatter.ofPattern(DATE_TIME_EN);
}
public static String format(LocalDateTime date, String pattern) {
return DateTimeFormatter.ofPattern(pattern).format(date);
}
public static String format(LocalDateTime date) {
return defaultFormatter.format(date);
}
public static LocalDateTime parse(String date, String pattern) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
return LocalDateTime.parse(date, dtf);
}
public static LocalDateTime parse(String date) {
return LocalDateTime.parse(date, defaultFormatter);
}
public static Long parseSecond(String date, String pattern) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
return getSecond(LocalDateTime.parse(date, dtf));
}
public static Long parseSecond(String date) {
return getSecond(LocalDateTime.parse(date, defaultFormatter));
}
public static Long parseMilli(String date, String pattern) {
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern);
return getMilli(LocalDateTime.parse(date, dtf));
}
public static Long parseMilli(String date) {
return getMilli(LocalDateTime.parse(date, defaultFormatter));
}
public static Long getSecond(LocalDateTime date) {
return date.toInstant(OffsetDateTime.now().getOffset()).getEpochSecond();
}
public static Long getMilli(LocalDateTime date) {
return date.toInstant(OffsetDateTime.now().getOffset()).toEpochMilli();
}
public static LocalDateTime getLocalDateTime(Long date) {
// 获取date的位数
int i = Long.toString(date).length();
if (i == 13) {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
} else if (i == 10) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(date), ZoneId.systemDefault());
} else {
throw new DateTimeException("不支持");
}
}
public static String getDateString(Long date, String pattern) {
LocalDateTime localDateTime = getLocalDateTime(date);
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
public static String getDateString(Long date) {
return getLocalDateTime(date).format(defaultFormatter);
}
public static boolean isAfter(LocalDateTime date, LocalDateTime other) {
return date.isAfter(other);
}
public static boolean isBefore(LocalDateTime date, LocalDateTime other) {
return date.isBefore(other);
}
public static boolean isEqual(LocalDateTime date, LocalDateTime other) {
return date.isEqual(other);
}
}