LocalDateTime与时间戳、日期字符串的转换
摘要:介绍LocalDateTime与时间戳、日期字符串的转换。
需求背景
服务器部署在不同时区,数据在业务使用过程中,需要进行时区切换,为了不影响数据效果,把各个时区的时间统一为UTC时区。故分享如何实现LocalDateTime与时间戳、日期字符串的转换。
LocalDateTime转字符串
可以把LocalDateTime转换成指定时区、指定格式的字符串,以UTC时区为例,转换成yyyy-MM-dd HH:mm:ss的实现逻辑如下:
private static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static ZoneId myZone = TimeZone.getTimeZone("UTC").toZoneId();
private static ZoneId currentZone = OffsetDateTime.now().getOffset();
/**
* 格式化 LocalDateTime 为UTC时区的字符串
*
* @param localTime
* @return utc 时间
* @Date 2023-08-05
**/
public static String getGivenZoneTimeStr(LocalDateTime localTime) {
// System.out.println("转换前的时间:" + localTime.format(df));
LocalDateTime newTime = localTime.atZone(currentZone).withZoneSameInstant(myZone).toLocalDateTime();
return newTime.format(df);
}
如果把myZone换成其它时区,则可以得到对应时区的时间,诸如GMT+7:00、GMT+8:00等。
LocalDateTime转时间戳
这篇文章很水,如果不是因为使用如下LocalDateTime转时间戳导致转换失败,也就不发此文了:
public static long local2Timestamp(LocalDateTime localTime) {
// 设置时区偏移量,这里设置为UTC
long milliSecond = localTime.toInstant(ZoneOffset.UTC).toEpochMilli();
System.out.println("local时间转UTC时间戳:" + milliSecond);
return milliSecond;
}
正确的LocalDateTime转时间戳实现代码如下:
/**
* UTC时区
*/
private static ZoneId myZone = TimeZone.getTimeZone("UTC").toZoneId();
private static ZoneId currentZone = OffsetDateTime.now().getOffset();
public static long local2TimestampPlus(LocalDateTime localTime) {
LocalDateTime newTime = localTime.atZone(currentZone).withZoneSameInstant(myZone).toLocalDateTime();
return newTime.toInstant(ZoneOffset.UTC).toEpochMilli();
}
时间戳转LocalDateTime
这个就简单了,请各位使用的时候根据需要设置一下时区,我这里使用默认时区验证:
public static LocalDateTime timestamp2Local(long timestamp) {
return Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime();
}
时间戳转日期字符串
Date对象保存的是毫秒数,本身不带时区信息。但是如果调用Date.toString()、Date.parse()等方法把Date展现出来时,就会存在时区的概念,需要进行时区转换,而且不是所有人都只想要UTC时间。
public static void main(String[] args) {
String result = timestamp2Str(Instant.now().toEpochMilli(), "GMT+7:00");
System.out.println(result);
}
public static String timestamp2Str(long timestamp, String timeZoneId) {
Date timeStampDate = new Date(timestamp);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone(timeZoneId));
return sdf.format(timeStampDate);
}
结束语
文章到这里就结束了,看完之后你有什么想法想要跟大家分享呢?评论区在等着你!
读后有收获,小礼物走一走,请作者喝咖啡。

作者:楼兰胡杨
本文版权归作者和博客园共有,欢迎转载,但请注明原文链接,并保留此段声明,否则保留追究法律责任的权利。
标签:
Java基础
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
2021-09-25 SpringBoot启动类没有启动按钮,java文件变为灰色的解决策略
2021-09-25 MQ面试题|Kafka如何实现每秒上百万的高并发写入【转】
2020-09-25 ActiveMQ消息投递方式之异步投递【转】
2020-09-25 ActiveMQ消息投递方式+死信队列
2020-09-25 Spring Boot 整合ActiveMQ实现延时发现消息
2019-09-25 MySQL group_concat 介绍
2019-09-25 MySQL 取分组后每组的最新记录