UTC时间和时间戳介绍

什么是UTC?

UTCCoordinated Universal Time 的缩写,中文叫:协调世界时。它是全球统一的时间基准。所有国家的时间,都是在 UTC 基础上加减偏移得到的,例如:

复制代码
UTC 时间:2026-02-28 12:00
那么:
	•	中国(UTC+8)→ 20:00
	•	美国东部(UTC-5)→ 07:00
	•	UTC-12 → 00:00

为什么需要UTC?

在分布式系统中:

  • 服务器可能在不同国家

  • 用户在不同地区

  • 日志需要统一时间基准

  • 数据库要避免时区混乱

所以最佳实践是:服务器统一使用UTC,展示时再转为本地时区。

示例:

复制代码
# linux使用date -u查看UTC
$ date
Thu Mar 12 15:39:46 CST 2026
$ date -u
Thu Mar 12 07:39:57 UTC 2026

#java中使用
Instant now = Instant.now();// 表示 UTC 时间线上的一个绝对时间点,它本身不包含时区信息
System.out.println(now); //2026-03-12T07:54:07.936329Z

ZonedDateTime displayTime = now.atZone(ZoneId.of("Asia/Shanghai")); //指定时区
System.out.println(displayTime.toString()); //2026-03-12T15:54:07.936329+08:00[Asia/Shanghai]


ZonedDateTime now1 = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
System.out.println(now1); //2026-03-12T15:54:07.936329+08:00[Asia/Shanghai]

时间戳和UTC的关系

时间戳本质是:从 1970-01-01 00:00:00 UTC 到当前时间的秒数或毫秒数。这个起点叫:Unix Epoch。它是基于 UTC 的,是一个绝对时间点。例如

复制代码
UTC时间:1970-01-01 00:00:00
时间戳:0

UTC时间:1970-01-01 00:00:01
时间戳:1

时间戳转时间字符串

时间戳转时间字符串两种方法:

1)标准流程:(推荐)

timestamp → Instant → ZonedDateTime / LocalDateTime (指定时区)

通过Instant的atZone指定时区设置时区,如果不指定默认使用JVM默认时区,示例:

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

long timestamp = 1710230400000L;
Instant instant = Instant.ofEpochMilli(timestamp);
ZonedDateTime time = instant.atZone(ZoneId.of("Asia/Shanghai"));
System.out.println(time); //2024-03-12T16:00+08:00[Asia/Shanghai]

//格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String result = time.format(formatter);
System.out.println(result); //2024-03-12 16:00:00

//可以一步完成
String result =
    Instant.ofEpochMilli(timestamp)
        .atZone(ZoneId.of("Asia/Shanghai"))
        .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

也可以通过DateTimeFormatter设置时区,例如:

java 复制代码
DateTimeFormatter ft = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault()); //Asia/Shangha
String formattedDate = ft.format(Instant.ofEpochMilli(ts)); //2024-03-12 16:00:00

2)JDK8之前写法:(不推荐)

java.util.Date类本身是不带时区信息的,它仅存储自1970年1月1日00:00:00 GMT以来的毫秒数(时间戳)。Date对象在显示时会根据JVM本地时区(TimeZone)自动调整,所以如果服务器在UTC,而你想要UTC+8就会产生8小时误差。当然,可以在格式化的时候指定时区,例如:

java 复制代码
#查看JVM默认时区:JVM Default TimeZone
System.out.println(TimeZone.getDefault().getID()); //JVM Default TimeZone: Asia/Shanghai

// 使用SimpleDateFormat处理时区
Date date = new Date(); 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shangha")); // 设置时区
System.out.println(sdf.format(date)); // 2024-03-12 16:00:00

什么是GMT

GMT是格林尼治时间,指位于英国伦敦郊区的皇家格林尼治天文台的标准时间。但由于较大的误差,现在基本不使用了。

相关推荐
Gofarlic_oms11 天前
利用API实现ANSYS许可证管理自动化集成
运维·服务器·开发语言·matlab·自动化·负载均衡
AI+程序员在路上1 天前
VS Code 完全使用指南:下载、安装、核心功能与 内置AI 编程助手实战
开发语言·人工智能·windows·开源
invicinble1 天前
这里对java的知识体系做一个全域的介绍
java·开发语言·python
catchadmin1 天前
使用 PHP TrueAsync 改造 Laravel 协程异步化的可行路径
开发语言·php·laravel
wbs_scy1 天前
【Linux 线程进阶】进程 vs 线程资源划分 + 线程控制全详解
java·开发语言
ss2731 天前
食谱推荐系统功能测试如何写?
java·数据库·spring boot·功能测试
AI人工智能+电脑小能手1 天前
【大白话说Java面试题】【Java基础篇】第15题:JDK1.7中HashMap扩容为什么会发生死循环?如何解决
java·开发语言·数据结构·后端·面试·哈希算法
try2find1 天前
打印ascii码报错问题
java·linux·前端
014-code1 天前
CompletableFuture 实战模板(超时、组合、异常链处理)
java·数据库
Nicander1 天前
多数据源下@transcation事务踩坑
java·后端