Java解析前端传来的Unix时间戳

在Java中,前端传递的 1749571200000 是一个 Unix时间戳(毫秒级) ,表示自1970年1月1日00:00:00 UTC以来经过的毫秒数。以下是两种常见的解析方式(推荐使用Java 8的java.time API):


方法1:使用 Java 8 的 java.time API(推荐)

java

复制

下载

复制代码
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class TimestampParser {
    public static void main(String[] args) {
        long timestamp = 1749571200000L; // 前端传来的时间戳
        
        // Step 1: 转换为 Instant (UTC时间)
        Instant instant = Instant.ofEpochMilli(timestamp);
        
        // Step 2: 转换为本地时间(根据系统时区)
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        
        // Step 3: 格式化输出(可选)
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = localDateTime.format(formatter);
        
        System.out.println("解析结果: " + formattedDate); // 输出: 2025-06-09 08:00:00
    }
}
关键点:
  • Instant.ofEpochMilli() 将毫秒时间戳转为 Instant(UTC标准时间)。

  • LocalDateTime.ofInstant() 结合时区(如 ZoneId.systemDefault())转换为本地时间。

  • 使用 DateTimeFormatter 自定义日期格式。


方法2:使用旧版 java.util.Date(兼容旧项目)

java

复制

下载

复制代码
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class LegacyTimestampParser {
    public static void main(String[] args) {
        long timestamp = 1749571200000L; // 前端传来的时间戳
        
        // Step 1: 创建Date对象
        Date date = new Date(timestamp);
        
        // Step 2: 设置日期格式化器(指定时区)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); // 按需修改时区
        
        // Step 3: 格式化输出
        String formattedDate = sdf.format(date);
        
        System.out.println("解析结果: " + formattedDate); // 输出: 2025-06-09 08:00:00
    }
}

时区说明

  • 前端时间戳 通常是UTC时间,解析时需要明确目标时区:

    • 如果前端传递的是UTC时间,解析时建议先转UTC再按需调整时区。

    • 示例中 Asia/Shanghai(UTC+8)会导致时间+8小时。

  • 关键时区设置:

    • Java 8: ZoneId.of("UTC")ZoneId.systemDefault()

    • 旧版API: sdf.setTimeZone(TimeZone.getTimeZone("UTC"))


验证时间戳

  • 工具验证:https://www.epochconverter.com/

  • 1749571200000 对应 UTC时间 2025-06-09 00:00:00

    转换为北京时间(UTC+8)则是 2025-06-09 08:00:00


总结

场景 方案
Java 8+ 项目 优先使用 Instant + LocalDateTime
兼容旧系统 (Java 7-) 使用 Date + SimpleDateFormat
时区敏感 务必显式设置目标时区
相关推荐
guodingdingh9 小时前
软件开发工作问题总结0718
java·开发语言·数据库
寅时码10 小时前
React 之死·终章:一个 useRef,把闭包陷阱、依赖数组、漫天 rerender 全送走
前端·react.js·ai编程
不好听61310 小时前
HTML 事件监听机制:从 DOM Level 0 到 React 合成事件
前端·react.js·html
wordbaby11 小时前
为什么刷新页面就 404?聊聊 SPA 路由与 Nginx 的那点事
前端
咖啡八杯11 小时前
GoF设计模式——解释器模式
java·后端·spring·设计模式
不好听61311 小时前
React 核心概念:JSX、组件与数据驱动
前端·react.js
触底反弹11 小时前
🔥 React 零基础入门(中):500 行屎山代码到组件化的蜕变
前端·javascript·react.js
不好听61311 小时前
React vs Vue:两大前端框架技术选型深度对比
前端·vue.js·react.js
GuWenyue11 小时前
Cursor黑盒拆解!1套LangChain.js手写Mini编程Agent,自动生成React项目,效率提升60%
前端·数据库·人工智能
GuWenyue11 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法