public final class DateTimeUtils {
public static final String PATTERN = "yyyy-MM-dd HH:mm:ss:SSS";
// 日期转Str
public static String date2Str(Date date) {
Instant instant = date.toInstant();
ZoneId zone = ZoneId.systemDefault();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zone);
DateTimeFormatter format = DateTimeFormatter.ofPattern(PATTERN);
String nowStr = localDateTime.format(format);
return nowStr;
}
// 日期转字符串
public static Date str2Date(String dateStr) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN);
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
Date date = Date.from(instant);
return date;
}
// 当天日期
public static String nowTimeStr() {
return date2Str(new Date());
}
// 字符串日期转时间戳
public static long getTimestamp(String dateStr) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN);
LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
ZoneId zone = ZoneId.systemDefault();
Instant instant = localDateTime.atZone(zone).toInstant();
return instant.toEpochMilli();
}
/**
* 获取当前毫秒数
*/
public static Long toEpochMilli(LocalDateTime dateTime) {
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
/**
* 判断是否为闰年
*/
public static boolean isLeapYear(LocalDate localDate) {
return localDate.isLeapYear();
}
}
网友评论