简体   繁体   English

如何将日期字符串转换为 joda DateTime?

[英]How to convert Date String to joda DateTime?

I have a date string "Wed Nov 20 00:00:00 IST 2019".我有一个日期字符串“Wed Nov 20 00:00:00 IST 2019”。 How do I convert it to joda DateTime with the pattern "yyyyMMdd".如何使用“yyyyMMdd”模式将其转换为 joda DateTime。


dateObject.setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd"));

The problem with your String pattern is, that JodaTime does not recognize the 'IST' timezone.您的字符串模式的问题是,JodaTime 无法识别“IST”时区。 (See http://joda-time.sourceforge.net/timezones.html for a list of supported time zones.) (有关支持的时区列表,请参阅http://joda-time.sourceforge.net/timezones.html 。)

If you always want to parse the date in the same time zone, you could use:如果您总是想在同一时区解析日期,您可以使用:

DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss 'IST' yyyy").withZone(DateTimeZone.forID("Indian/Mahe"));
DateTime parsed = DateTime.parse("Wed Nov 20 00:00:00 IST 2019", dateTimeFormatter);

Note that I have used IST as a string literal in the pattern format, ie, this will only work if your date strings always includes the "IST" string.请注意,我使用IST作为模式格式中的字符串文字,即,这仅在您的日期字符串始终包含“IST”字符串时才有效。

To add fixed time zone information to your parsed date use withZone on the formatter.要将固定时区信息添加到您的解析日期,请使用格式化程序上的withZone I picked a random Indian timezone known to JodaTime, "Indian/Mahe" in this case.我随机选择了一个 JodaTime 已知的印度时区,在本例中为“Indian/Mahe”。 Look up the one that matches your time zone in the list of supported time zones.在支持的时区列表中查找与您的时区匹配的时区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM