java中时间戳与Date
- 时间戳转换为Data
public static void timestamp2Date() { long timeMillis = System.currentTimeMillis(); Date date = new Date(timeMillis); }
- Data转换为时间戳
public static void date2Timestamp() { Date date = new Date(); long timeMillis = date.getTime(); }
时间戳与LocalDateTime相互转换
- 时间戳转LocalDateTime
public static void timestamp2LocalDateTime() { long timeMillis = System.currentTimeMillis(); ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime localDateTime = Instant.ofEpochMilli(timeMillis).atZone(zoneId).toLocalDateTime(); }
- LocalDateTime转时间戳
public static void localDateTime2Timestamp() { ZoneId zoneId = ZoneId.systemDefault(); LocalDateTime now = LocalDateTime.now(); long epochMilli = now.atZone(zoneId).toInstant().toEpochMilli(); }