Is Simpledateformat In Java And Android Different?
Solution 1:
I can't tell about Android, but in the JVM, the Z
pattern recognizes only offsets without :
, such as -0500
, while the X
pattern can recognize -05:00
: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Perhaps Android has a different implementation, where Z
also recognizes offsets with :
, such as -05:00
.
I'm afraid there's no one-pattern-fits-all solution: Z
doesn't work in JVM, X
doesn't work in Android, how to make it work in both? I think the best you can do is to try both patterns, something like this (in pseudocode):
try {
parse with X
} catch(Exception e) {
X didn't work, parse with Z
}
A much better alternative is to use the threeten backport and configure with ThreetenABP - or use java.time
classes if your API level is 26:
// parse inputOffsetDateTimeodt= OffsetDateTime.parse("2018-04-27T22:31:07.962-05:00");
// convert to java.util.Date (threeten backport)Datedate= DateTimeUtils.toDate(odt.toInstant());
// or, if you have java.time (API level 26)Datedate= date.from(odt.toInstant());
Solution 2:
And according to your example and the android doc, you should use X
(ISO8601) instead of Z
(RFC822)
That gives: "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"
Post a Comment for "Is Simpledateformat In Java And Android Different?"