我正在尝试将当前日期与以“EEE MMM dd HH:mm:ss zzz yyyy”格式接收的输入日期进行比较
当我的输入字符串是 Wed Apr 01 09:00:00 GMT 2020
时,这段代码有效。但如果是 Wed Apr 01 09:00:00 BST 2020
则不起作用。
DateTime currentTime =DateTime.now().withZone(DateTimeZone.forID("Europe/London"));
DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss zzz yyyy");
DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 BST 2020");
if (inputDateTime.isBefore(currentTime))
Log.d(TAG, "if ");
else
Log.d(TAG, "else ");
知道我做错了什么吗?另外,请随意建议是否有更好的方法(不能使用新的 Java 日期和时间库,因为我们支持 android API 19+)
最佳答案
如果您使用ThreeTen Android Backport而不是Joda-Time
,你应该这样做,然后它解析得很好:
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss zzz yyyy");
ZonedDateTime inputDateTime = ZonedDateTime.parse("Wed Apr 01 09:00:00 BST 2020", fmt);
System.out.println(inputDateTime);
输出
2020-04-01T09:00+01:00[Europe/Isle_of_Man]
关于java - Joda 时间为 BST 时区提供了解析异常,但在 GMT 时则没有,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61133031/