我有一个日期字符串“Wed Nov 20 00:00:00 IST 2019”。如何将其转换为具有模式“yyyyMMdd”的 joda DateTime。
dateObject.setStartDate(new DateTime().plusDays(1).toString("yyyyMMdd"));
最佳答案
您的字符串模式的问题是,JodaTime 无法识别“IST”时区。 (有关受支持时区的列表,请参阅 http://joda-time.sourceforge.net/timezones.html。)
如果你总是想解析同一时区的日期,你可以使用:
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);
请注意,我已使用 IST
作为模式格式中的字符串文字,即,只有当您的日期字符串始终包含“IST”字符串时,这才有效。
要将固定时区信息添加到解析的日期中,请在格式化程序上使用withZone
。我随机选择了 JodaTime 已知的印度时区,在本例中为“Indian/Mahe”。在支持的时区列表中查找与您的时区匹配的时区。
关于java - 如何将日期字符串转换为joda日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58951569/