用奇怪的时区解析 Java 中的日期
我收到以下格式发送到我的应用程序的日期:
yyyy-MM-dd'T'HH:mm:ss.SSS-04:00
问题是时区,它不遵循 Z 格式 (-0400),也不遵循 z 格式 (GMT-04:00)。
我是否缺少其他格式选项,或者我可以输入解析这些时区的方法吗?
I'm getting dates sent to my application in the following format:
yyyy-MM-dd'T'HH:mm:ss.SSS-04:00
The problem is the TimeZone, it doesn't follow the Z format (-0400) and it doesn't follow the z format (GMT-04:00).
Is there another format option I'm missing or can I input a way to parse these TimeZones?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑切换到 Joda time - 它比 java 本机日期库提供了更多的灵活性和可读性,包括处理时区风格(这是我切换的关键原因之一)。
Consider switching to Joda time - it offers a lot more flexibility and readability than the java native date libraries, including handling that time zone style (that's one of the key reasons I switched).
这种简单的方法开发起来很快而且很简单。
The naive approach is quick to develop and simple.
您似乎获得了 XSD 日期时间格式。如果您想坚持使用纯开箱即用的 Java,请查看 javax.xml.datatype.XMLGregorianCalendar,它是为处理此类事情而构建的。
然而,正如另一位发帖者所说,使用第三方库(例如 Joda time)可能会更容易。 Java Calendar 对象使用起来总是有点麻烦。
You appear to be getting an XSD dateTime format. If you want to stick with pure out of the box Java look at javax.xml.datatype.XMLGregorianCalendar which was built to handle that sort of thing.
However as another poster has said you'd probably have an easier time using a third party library such as Joda time. Java Calendar objects are always a bit cumbersome to use.