Joda时间在BST时区解析时会引发异常,但在GMT时区不会。

huangapple go评论91阅读模式
英文:

Joda time gives parse Exception for BST timezone but not for GMT

问题

以下是翻译好的部分:

"我正在尝试将当前日期与以此格式接收的输入日期进行比较:'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 ");

"我在做错了什么?还有没有更好的方法可以做到这一点(因为我们支持 Android API 19+,所以不能使用新的 Java 日期和时间库)?"

英文:

I'm trying to compare current date with an input date, received in this format "EEE MMM dd HH:mm:ss zzz yyyy"

This piece of code works when my input string is Wed Apr 01 09:00:00 GMT 2020 but doesn't work if its 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 ");

Any idea on what am I doing wrong? Also, feel free to suggest if there's a better way to do it (can't use new Java date and time library, since we support android API 19+ )

答案1

得分: 2

如果您使用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]
英文:

If you use ThreeTen Android Backport instead of Joda-Time, and you should, then it parses fine:

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);

Output

2020-04-01T09:00+01:00[Europe/Isle_of_Man]

答案2

得分: 1

文档提到,在joda DateTimeFormat中无法解析时区名称,因为时区缩写存在歧义,解析器无法准确知道是哪个时区。

时区名称:无法解析时区名称('z')。

例如:

  • BST 可以是 British Summer Time 或者 Bangladesh Standard Time 或者 Bougainville Standard Time

  • PST 可以是 Pacific Standard Time 或者 Pakistan Standard Time

  • CST 可以是 Central Standard Time (美国)、China Standard Time 或者 Cuba Standard Time

  • EST 可以是 Eastern Standard Time (美国) 或者 Eastern Standard Time (澳大利亚)。

最佳方式是将 BST 替换为适当的 ISO 时区代码这里(例如 Europe/Isle_of_Man),然后简单地使用 "EEE MMM dd HH:mm:ss ZZZ yyyy" 作为 DateTimeFormat:

DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss ZZZ yyyy");
DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 Europe/Isle_of_Man 2020");
    
System.out.println(inputDateTime);  //2020-04-01T09:00:00.000+01:00
英文:

Docs has mentioned that Time zone names cannot be parsed in joda DateTimeFormat because time zone abbreviations are ambiguous and the parser can't know which time zone it is exactly

Zone names: Time zone names ('z') cannot be parsed.

For example

  • BST can be British Summer Time or Bangladesh Standard Time or Bougainville Standard Time

  • PST can be Pacific Standard Time or Pakistan Standard
    Time

  • CST could be Central Standard Time (USA), China Standard Time, or Cuba Standard Time

  • EST could be Eastern Standard Time (USA), or Eastern Standard Time (Australia).

And the best way to is to replace BST with appropriate iso timezone code here (for example Europe/Isle_of_Man), and then simple use "EEE MMM dd HH:mm:ss ZZZ yyyy" DateTimeFormat

DateTimeFormatter fmt = DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss ZZZ yyyy");
DateTime inputDateTime = fmt.parseDateTime("Wed Apr 01 09:00:00 Europe/Isle_of_Man 2020");
    
System.out.println(inputDateTime);  //2020-04-01T09:00:00.000+01:00

答案3

得分: 0

我不确定这是否满足您对Android API 19+的要求,但以下是关于使用SimpleDateFormatter进行日期格式化的内容:

String pattern = "EEEEE MMMMM yyyy HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String date = simpleDateFormat.format(new Date());
System.out.println(date);

希望这能解决您的问题!

英文:

I'm not sure if this meets your requirements for the Andriod API 19+, but here is something for the date formatting with SimpleDateFormatter

    String pattern = "EEEEE MMMMM yyyy HH:mm:ss";
	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
	String date = simpleDateFormat.format(new Date());
	System.out.println(date);

Hope this solves your problem!

huangapple
  • 本文由 发表于 2020年4月10日 10:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61133031.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定