Closed t1r closed 4 years ago
I cannot reproduce. With the latest on master
I can add the following to the sample app:
String input = "Thu Dec 31 1998 23:22:50 GMT+0300 (CET)";
String inputPattern = "E MMM dd yyyy HH:mm:ss 'GMT'Z (z)";
LocalDateTime ldt = LocalDateTime.parse(input,
DateTimeFormatter.ofPattern(inputPattern, Locale.US));
tv.setText(ldt.toString());
and it displays the correct value:
Please provide a failing test case or executable sample that demonstrates the problem.
Hello, I think the problem is in parsing some of the Time Zone Abbreviations, like BST, MSK and so on. Here are test cases:
//org.threeten.bp.format.DateTimeParseException: Text 'Tue Nov 19 2019 00:00:00 GMT+0300 (MSK)' could not be parsed at index 35
@Test public void test_parse_tzdbGmtZone_MSK() {
AndroidThreeTen.init(context);
String dateString = "Tue Nov 19 2019 00:00:00 GMT+0300 (MSK)";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
}
//DateTimeParseException
@Test public void test_parse_tzdbGmtZone_BST() {
AndroidThreeTen.init(context);
String dateString = "Tue Nov 19 2019 00:00:00 GMT+0100 (BST)";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
}
//DateTimeParseException
@Test public void test_parse_tzdbGmtZone_NCT() {
AndroidThreeTen.init(context);
String dateString = "Tue Nov 19 2019 00:00:00 GMT+0100 (NCT)";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
}
//pass
@Test public void test_parse_tzdbGmtZone_CET() {
AndroidThreeTen.init(context);
String dateString = "Tue Nov 19 2019 00:00:00 GMT+0100 (CET)";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("E MMM dd yyyy HH:mm:ss 'GMT'Z (z)", Locale.US);
LocalDateTime parsed = LocalDateTime.parse(dateString, formatter);
assertEquals(parsed, LocalDateTime.of(2019, 11, 19, 0, 0, 0, 0));
}
All of them are pass in the ThreeTen lib. Thank You.
Hello @JakeWharton, I researched this issue more deeper. And found out a reason.
Briefly speaking problem is in getDisplayName()
method of java.util.TimeZone
, which has a different implementation for Android and Java.
Comment, from source code of androids TimeZone implementation.
// BEGIN Android-changed: implement using android.icu.text.TimeZoneNames
More details:
We call getDisplayName()
in the @Override public int parse(DateTimeParseContext context, CharSequence text, int position)
of DateTimeFormatterBuilder
class for build a zone ids map, that is used for parse time zone.
As a result, we have maps of size: 631 - in android 880 - in java
Android map doesn't contain "MSK", "BST", "NCT" and a lot of another time zone ids. What is the cause of DateTimeParseException
.
How can we fix it - i don't know. It looks rather complicated, because we need to rewrite some of org.threeten.bp classes to change implementation of getDisplayName()
specially for android.
Best wishes.
PS: Sorry for my bad english.
This library is just a repackaging of the timezone database, it's not an Android-specific version of the library. The failures you're seeing would occur with usage of the normal ThreeTenBP library if you us it on Android. Working around this problem is going to be out-of-scope for this library. A fix would have to be pursued directly in ThreeTenBP.
An exception occurs when I try to parse a date and time with this pattern:
E MMM dd yyyy HH:mm:ss 'GMT'Z (z)
With the standard java library (java.time.format.DateTimeFormatter) and ThreeTen library, the code works correctly.
Code:
Exception: