package cn.zytao.taosir.common.utils; import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.util.Date; public class DateTimeUtils { public static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HHmmss"); public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd"); public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMddHHmmss"); /** * 获取当前系统时间 * @return */ public static LocalTime getLocalTime() { return LocalTime.now(); } /** * 获取当前系统日期 * @return */ public static LocalDate getLocalDate() { return LocalDate.now(); } /** * 获取当前系统日期时间 * @return */ public static LocalDateTime getLocalDateTime() { return LocalDateTime.now(); } /** * 获取当前系统时间字符串 * @return */ public static String getLocalTimeString() { return LocalTime.now().format(TIME_FORMATTER); } /** * 获取当前系统日期字符串 * @return */ public static String getLocalDateString() { return LocalDate.now().format(DATE_FORMATTER); } /** * 获取当前系统日期时间字符串 * @return */ public static String getLocalDateTimeString() { return LocalDateTime.now().format(DATETIME_FORMATTER); } /** * 字符串转LocalTime * @param time * @return */ public static LocalTime string2LocalTime(String time) { return LocalTime.parse(time, TIME_FORMATTER); } /** * 字符串转LocalDate * @param date * @return */ public static LocalDate string2LocalDate(String date) { return LocalDate.parse(date, DATE_FORMATTER); } /** * 字符串转LocalDateTime * @param dateTime * @return */ public static LocalDateTime string2LocalDateTime(String dateTime) { return LocalDateTime.parse(dateTime, DATETIME_FORMATTER); } /** * Date转LocalDateTime * @param date * @return */ public static LocalDateTime date2LocalDateTime(Date date) { Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。) ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区) LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime(); return localDateTime; } /** * Date转LocalDate * @param date * @return */ public static LocalDate date2LocalDate(Date date) { Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。) ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区) LocalDate localDate = instant.atZone(zoneId).toLocalDate(); return localDate; } /** * Date转LocalDate * @param date * @return */ public static LocalTime date2LocalTime(Date date) { Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。) ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区) LocalTime localTime = instant.atZone(zoneId).toLocalTime(); return localTime; } /** * LocalDateTime转换为Date * @param localDateTime */ public static Date localDateTime2Date(LocalDateTime localDateTime){ ZoneId zoneId = ZoneId.systemDefault(); ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a ZonedDateTime. Date date = Date.from(zdt.toInstant()); return date; } }