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

查看:678
本文介绍了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说

使用的格式是输出
的全部值的最短值,其中省略的部分隐含为零

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

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天全站免登陆