如何在 Java 中将 2010-12-15T16:26:49.841-08:00 转换为 GregorianCalendar?
我有一个字符串日期“2010-12-15T16:26:49.841-08:00”,我需要将其转换为Java中的GregorianCalendar。你如何做到这一点?
Jesper 的答案中的解决方案
使用 joda 时间的解决方案代码:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withOffsetParsed();
DateTime date = formatter.parseDateTime("2010-12-15T16:26:49.841-08:00");
I have a string date "2010-12-15T16:26:49.841-08:00" and I need to convert it to a GregorianCalendar in Java. How do you do this?
Solution from Jesper's answer
Code for the solution using joda time:
DateTimeFormatter formatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ").withOffsetParsed();
DateTime date = formatter.parseDateTime("2010-12-15T16:26:49.841-08:00");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,标准
SimpleDateFormat
类无法很好地处理 ISO 8601 格式。具体来说,它无法处理末尾时区偏移中的:
。您可以做的是手动从时区偏移中删除
:
,以便获得如下所示的字符串:(请注意,时区偏移是
-0800
而不是-08:00
)。然后,您可以使用SimpleDateFormat
解析它,格式为yyyy-MM-dd'T'HH:mm:ss.SSSZ
。但最好使用流行的 Joda-Time 库来处理时间和日期;它比标准 Java API 日期和日历类要好得多,并且可以正确处理 ISO 8601 格式。
Unfortunately, the standard
SimpleDateFormat
class cannot handle ISO 8601 format very well. Specifically, it cannot handle the:
that is in the timezone offset at the end.What you can do is manually remove the
:
from the timezone offset, so that you get a string that looks like this:(note that the timezone offset is
-0800
instead of-08:00
). Then you can parse it withSimpleDateFormat
with the formatyyyy-MM-dd'T'HH:mm:ss.SSSZ
.But it is better to use the popular Joda-Time library to handle times and dates; it is much better than the standard Java API date and calendar classes and handles ISO 8601 format properly.
使用 SimpleDateFormat,请参阅此处的 javadocs:
然后将日期转换为日历。在这里查看大量示例:
Use SimpleDateFormat, see javadocs here:
Then convert the Date to Calendar. Take a look at plenty of examples here:
您可以尝试使用这段代码
这肯定会为您提供公历日历对象
You can try with this piece of code
This will defnitely give you the Gregorian Calender Object
其他答案是正确的,但现在已经过时了。他们使用麻烦的旧类,现在已被 java.time 框架取代。
使用 java.time
输入字符串恰好符合 ISO 8601 标准格式。因此,无需指定格式化模式,因为 java.time 类在解析/生成字符串时默认使用 ISO 8601。
输入字符串包含相对于 UTC 的偏移量,因此我们解析为
OffsetDateTime
。如果您心中有一个特定的时区,而不仅仅是与 UTC 的偏移量,请应用它。
GregorianCalendar
您应该避免使用旧的日期时间类,例如
GregorianCalendar
。但如果您必须与尚未更新 java.time 的旧代码进行互操作,您可以进行转换。使用添加到旧类中的新方法进行转换。有关更多信息和漂亮的图表,请参阅我的问题和回答。关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了旧的麻烦的日期时间类,例如 java.util.Date、.Calendar 和 & 。
java.text.SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。
许多 java.time 功能都向后移植到 Java 6 和 Java 6。 ThreeTen-Backport 中的 7 并进一步适应 Android 在 ThreeTenABP。
ThreeTen-Extra 项目通过附加类扩展了 java.time。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
Interval
、YearWeek
、YearQuarter
等。The other Answers are correct but now outdated. They use troublesome old classes now supplanted by the java.time framework.
Using java.time
The input string happens to comply with ISO 8601 standard formatting. So no need to specify a formatting pattern as the java.time classes use ISO 8601 by default when parsing/generating strings.
The input string includes an offset-from-UTC, so we parse as an
OffsetDateTime
.If you have a specific time zone in mind, rather than a mere offset-from-UTC, apply that.
GregorianCalendar
You should avoid the old date-time classes such as
GregorianCalendar
. But if you must to interoperate with old code not yet updated for java.time, you can convert. Use new methods added to the old classes for conversion. For more info and a nifty diagram, see my Question and Answer.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the old troublesome date-time classes such as
java.util.Date
,.Calendar
, &java.text.SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP.
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.