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);
}
结束语
文章到这里就结束了,看完之后你有什么想法想要跟大家分享呢?评论区在等着你!
LocalDateTime与时间戳、日期字符串的转换的更多相关文章
- Java时间日期字符串格式转换大全
import java.text.*; import java.util.Calendar; public class VeDate { /** * 获取现在时间 * * @return 返回时间类型 ...
- java 日期字符串互相转换
一.把日期转换成字符串 //获取当前时间 Date date = new Date(); //打印date数据类型 System.out.println(date.getClass().get ...
- json里的日期字符串 怎么 转换成 javascript 的 Date 对象?
“/Date(1232035200000)/” 怎么转换成 javascript 的 Date 对象 做法:new Date(+/\d+/.exec(value)[1]); value就是json字 ...
- MFC中 日期字符串的转换
一.将字符串2011-08-1800:00:00转换为字符串2011-8-18,通过以下的函数 CString DataDeleteZero(CString DATA) { CStringstrmon ...
- kotlin --- 时间戳与字符串互相转换
直接贴代码,清晰易懂.喜欢点个赞 class Timestamp { /** * Timestamp to String * @param Timestamp * @return String */ ...
- mysql 日期 字符串 时间戳转换
#时间转字符串 select date_format(now(), '%Y-%m-%d'); -02-27 #时间转时间戳 select unix_timestamp(now()); #字符串转时间 ...
- MySQL日期 字符串 时间戳互转
平时比较常用的时间.字符串.时间戳之间的互相转换,虽然常用但是几乎每次使用时候都喜欢去搜索一下用法:本文将作为一个笔记,整理一下三者之间的 转换(即:date转字符串.date转时间戳.字符串转dat ...
- SQL Server日期时间格式转换字符串详解 (详询请加qq:2085920154)
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- SQL Server日期时间格式转换字符串
在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...
- SQL to_char,to_date日期字符串转换问题
1.转换函数 与date操作关系最大的就是两个转换函数:to_date(),to_char() to_date() 作用将字符类型按一定格式转化为日期类型: 具体用法:to_date('2004-11 ...
随机推荐
- Unable to Connect: sPort: 0 C# ServiceStack.Redis 访问 redis
需求: 对数据库中的不断抓取的文章进行缓存,因此需要定时访问数据,写入缓存中 在捕获到的异常日志发现错误:Unable to Connect: sPort: 0 使用的访问方式是线程池的方式:Poo ...
- php不使用mysqlDump工具实现的mysql数据备份
再无法使用mysqlDump等外部工具的时候,我们需要到处数据库备份的话,借助phpMyAdmin强大的功能就可以实现.如果我们想自己实现一个类似phpMysql的功能要如何去考虑了,因此,在这里我给 ...
- node_modules/@umijs/runtime" does not exist in container.
使用 umi 脚手架搭建项目,启动时报错 node_modules/@umijs/runtime" does not exist in container. 出现问题 .umi 是临时文件夹 ...
- directory 用于数据泵 导入、导出创建的目录。
1.查询directory目录 select * from dba_directories; 2.创建或者修改 directory目录 create or replace directory 目录名称 ...
- 用Docker Swarm实现容器服务高可用
背景与技术选择 根据我之前的几篇「Django 系列」文章,后端架构中我使用了 Django + Celery + RabbitMQ 三个框架/服务.现在有几个问题: 如何用容器快速部署这三个应用? ...
- 容器到底是个啥?(附Docker学习资源汇总)
目录Docker与容器 初识容器与Docker 为什么要使用Docker Docker优势简介Docker核心概念 Docker客户端和服务器 Docker镜像 D ...
- Linux下Oracle client(sqlplus)安装和配置
分类专栏: Linux 数据库 文章标签: Linux Oracle Client sqlplus 方向键版权1.下载rpm包http://www.oracle.com/technetwork/top ...
- 再说PG的连接
前面说过连接PG的方法,但是遇到问题又不通了. 按照前面的做法还是不行,正是鼻子气歪了. 到pg老家下载PGODBC,安装了,还是不行. 其实仅仅copy一个libpg.dll是不够的.因为libpg ...
- 分享 3 款基于 .NET 开源且免费的远程桌面工具
前言 今天大姚给大家分享 3 款基于 .NET 开源.免费.功能强大的远程桌面工具,希望可以给大家的远程工作和学习带来便利. 1Remote 1Remote是一款基于 .NET 开源(GPL-3.0 ...
- SpringBoot整合Redis日志反复提示Redis重连问题
1. 报错信息如图: 2. 原因: spring boot 2.0之后spring-boot-starter-data-redis默认不再使用jedis连接redis,而是lettuce 这是lett ...