| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- package com.crm.manager.util;
- import java.time.Instant;
- import java.time.LocalDateTime;
- import java.time.ZoneId;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
- public class DateUtils {
- /**
- * Date 转 秒级时间戳(数据库存的就是这个)
- */
- public static long dateToSecondTimestamp(Date date) {
- if (date == null) {
- return 0;
- }
- return date.getTime() / 1000; // 毫秒 → 秒(关键)
- }
- // 常用格式:yyyy-MM-dd HH:mm:ss
- public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
- /**
- * 秒级时间戳 → 日期字符串
- * @param secondTimestamp 秒级时间戳 (如 1735458266)
- * @return yyyy-MM-dd HH:mm:ss
- */
- public static String secondToDateTimeStr(long secondTimestamp) {
- if (secondTimestamp <= 0) {
- return null;
- }
- // 秒级时间戳转 Instant
- Instant instant = Instant.ofEpochSecond(secondTimestamp);
- // 转东八区时间(中国时区)
- LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.of("Asia/Shanghai"));
- // 格式化字符串
- return localDateTime.format(FORMATTER);
- }
- }
|