使用 Joda Time 库将日期字符串转换为 DateTime 对象
我有一个日期作为字符串,格式如下:“04/02/2011 20:27:05”
。我正在使用 Joda-Time 库,并希望将其转换为 DateTime
对象。我做到了:
DateTime dt = new DateTime("04/02/2011 20:27:05")
但我收到以下错误:
Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"
如何将上述日期转换为 DateTime
对象?
I have a date as a string in the following format "04/02/2011 20:27:05"
. I am using Joda-Time library and would like to convert it to DateTime
object. I did:
DateTime dt = new DateTime("04/02/2011 20:27:05")
But I'm getting the following error :
Invalid format: "04/02/2011 14:42:17" is malformed at "/02/2011 14:42:17"
How to convert the above date to a DateTime
object?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
一个简单的方法:
An simple method :
有两种方法可以实现。
日期时间格式
简单日期格式
There are two ways this could be achieved.
DateTimeFormat
SimpleDateFormat
使用
DateTimeFormat
:Use
DateTimeFormat
:我知道这是一个老问题,但我想补充一点,从 JodaTime 2.0 开始,你可以用一句话来做到这一点:
I know this is an old question, but I wanted to add that, as of JodaTime 2.0, you can do this with a one-liner:
从评论中我选择了一个答案,并添加了时区:
From comments I picked an answer like and also adding TimeZone:
您的格式不是预期的 ISO 格式,您应该尝试
Your format is not the expected ISO format, you should try
您还可以使用 SimpleDateFormat,如
日期时间格式
You can also use SimpleDateFormat, as in
DateTimeFormat
tl;dr
java.time
现代方法使用 java.time 类来取代古老的 Joda-Time 项目。
解析为
LocalDateTime
< /a> 因为您的输入缺少任何时区指示符或与 UTC 的偏移量。提示:如果可能,请使用标准 ISO 8601 将日期时间值交换为文本时的格式,而不是此处看到的格式。方便的是,java.time 类在解析/生成字符串时使用标准格式。
关于 java.time
java.time 框架内置于 Java 8 及更高版本中。这些类取代了麻烦的旧遗留日期时间类,例如
java.util.Date
,日历
, & ;SimpleDateFormat
。Joda-Time 项目,现已在 维护模式,建议迁移到 java.time 类。
要了解更多信息,请参阅 Oracle 教程。并在 Stack Overflow 上搜索许多示例和解释。规范为 JSR 310。
从哪里获取 java.time 类?
ThreeTen-Extra 项目通过附加类扩展了 java.time 。该项目是 java.time 未来可能添加的内容的试验场。您可能会在这里找到一些有用的类,例如
间隔
,YearWeek
,<代码>YearQuarter,以及更多 。tl;dr
java.time
The modern approach uses the java.time classes that supplant the venerable Joda-Time project.
Parse as a
LocalDateTime
as your input lacks any indicator of time zone or offset-from-UTC.Tip: Where possible, use the standard ISO 8601 formats when exchanging date-time values as text rather than format seen here. Conveniently, the java.time classes use the standard formats when parsing/generating strings.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
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.您需要一个适合于您正在使用的格式。查看文档以获取有关如何构建的说明。
即兴,我认为你需要
format = DateTimeFormat.forPattern("M/d/y H:m:s")
You need a DateTimeFormatter appropriate to the format you're using. Take a look at the docs for instructions on how to build one.
Off the cuff, I think you need
format = DateTimeFormat.forPattern("M/d/y H:m:s")