英文:
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 beBritish Summer Time
orBangladesh Standard Time
orBougainville Standard Time
-
PST
can bePacific Standard Time
orPakistan Standard
Time -
CST
could beCentral Standard Time
(USA),China Standard Time
, orCuba Standard Time
-
EST
could beEastern Standard Time
(USA), orEastern 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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论