字符串到 ZonedDateTime 正在改变格式 [英] String to ZonedDateTime is changing format

查看:35
本文介绍了字符串到 ZonedDateTime 正在改变格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String ip="2011-05-01T06:47:35.422-05:00";
ZonedDateTime mzt = ZonedDateTime.parse(ip).toInstant().atZone(ZoneOffset.UTC);
System.out.println(mzt);

System.out.println("-----");

String ip2="2011-05-01T00:00:00.000-05:00";
ZonedDateTime mzt2 = ZonedDateTime.parse(ip2).toInstant().atZone(ZoneOffset.UTC);
System.out.println(mzt2);

输出:

2011-05-01T11:47:35.422Z
-----
2011-05-01T05:00Z

为什么在情况 2 中日期格式会发生变化?由于这个原因,我收到 SQLServer 数据库错误.

Why is the date format getting changed in case 2? I am getting SQLServer Database error due to this.

推荐答案

这就是 toString 从文档说

所使用的格式将是输出完整值的最短格式省略部分隐含为零的时间.

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

要解决这个问题,你需要另一个格式化程序:

To solve this problem, you need another formatter :

String result = mzt2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"));

输出

2011-05-01T11:47:35.422Z
2011-05-01T05:00:00:000Z

这篇关于字符串到 ZonedDateTime 正在改变格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆