java.time
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("EEEE, MMMM d", Locale.ENGLISH);
String stringFromFirestore = "2018-05-22T10:30:00+00:00";
ZonedDateTime dateTime = OffsetDateTime.parse(stringFromFirestore)
.atZoneSameInstant(ZoneId.systemDefault());
System.out.println("hour: " + dateTime.toLocalTime());
System.out.println("date: " + dateTime.format(dateFormatter));
I ran the code in my time zone, Europe/Copenhagen, and got the following output since I am at offset +02:00 in May:
hour: 12:30
date: Tuesday, May 22
The string you got from Firestore is in ISO 8601 format, the international standard. OffsetDateTime
and other java.time
classes parse ISO 8601 as their default, that is, without any explicit formatter. I am also using LocalTime.toString()
for producing the time of day in ISO 8601 format since this seems to agree with what you wanted.
Question: Can I use java.time on Android?
Yes, java.time
works nicely on older and newer Android devices. It just requires at least Java 6.
- In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
- In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
- On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from
org.threeten.bp
with subpackages.
Links