首页 > 基础资料 博客日记
JAVA 时区时间相互转换
2023-07-24 12:40:01基础资料围观537次
//系统设置为UTC时区
System.setProperty("user.timezone", "UTC");
//String dateString = "2023-04-06T07:23:36.539767500";
String dateString = "2023-04-06T08:15:06.662Z";
System.out.println("UTC 时间:"+dateString.subSequence(0, dateString.length()));
String localDateTime1 = LocalDateTime.ofInstant(Instant.parse(dateString.subSequence(0, dateString.length())), TimeZone.getTimeZone("Asia/Shanghai").toZoneId()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
System.out.println("转换为北京时间:"+localDateTime1);
//获取当前时间和时区
String localDateTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
TimeZone timeZone = TimeZone.getDefault();
System.out.println("当前时区:"+timeZone.toZoneId()+",当前时间:"+localDateTime);
//获取当前时间转换为UTC时区
String utctime = LocalDateTime.now(TimeZone.getTimeZone("UTC").toZoneId()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
System.out.println("当前时区:"+TimeZone.getTimeZone("UTC").toZoneId()+",当前时间:"+utctime);
//获取当前时间转换为UTC时区
String time1 = LocalDateTime.now(TimeZone.getTimeZone("Asia/Tokyo").toZoneId()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
System.out.println("当前时区:"+TimeZone.getTimeZone("Asia/Tokyo").toZoneId()+",当前时间:"+time1);
//获取当前时间转换为特定时区
String transtime = LocalDateTime.now(TimeZone.getTimeZone("Asia/Shanghai").toZoneId()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"));
System.out.println("当前时区:"+TimeZone.getTimeZone("Asia/Shanghai").toZoneId()+",当前时间:"+transtime);
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- springboot~3.x项目中使用集成测试
- Java测试类、工具类与JavaBean对比解析
- SpringBoot-日志
- springboot~http2的支持
- 解疑释惑 - 日志体系之 slf4j + logback 组合(一)
- Web server failed to start. Port 8080 was already in use. 端口被占用
- Springboot 项目配置多数据源
- 伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 05
- 剑指offer-23、搜索⼆叉树的后序遍历序列
- 一个表示金额的数字是 100000000L,这是多少米?