-4

I used the search function but there was just too much information with too many different methods. I just want to solve this problem, after 1 hour of searching.

I get a string like this from Firestore: 2018-05-22T10:30:00+00:00 (can change if needed)

Now I want to change the time to the local timezone on the device. At the end I need 2 different strings like these:

hour: 10:30
date: Tuesday, May 22
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
dp12
  • 1
  • 2
  • Take a look at this answer https://stackoverflow.com/questions/50368493/how-to-parse-yyyy-mm-ddthhmmss-sssxxx-date-format-to-simple-in-android/50368877#50368877 – E.Abdel May 22 '18 at 14:34
  • 1
    Welcome to Stack Overflow. If there are too many questions and answers already, isn’t your question more a part of the problem than a part of the solution? You are asking two or three questions in one, try to avoid that. (1) How to parse the string from Firestone (2) How to format the time (3) How to format the date. It’ll be easier for everyone to handle one question at a time. – Ole V.V. May 22 '18 at 14:49
  • Stack Overflow is unsuited for open-ended questions like “how do I perform this or that task?” Also see [Why is “Can someone help me?” not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) If you tell us which information you found, why you found it unsuitable, what you tried and what went wrong, you’ll put us into a much better position for helping you. Which we’d love to do. – Ole V.V. May 22 '18 at 14:57
  • 1
    Duplicate of [*Converting ISO 8601-compliant String to java.util.Date*](https://stackoverflow.com/q/2201925/642706). Duplicate of [*How to Pick Timezone from ISO 8601 format String into a Calendar instace*](https://stackoverflow.com/q/46057203/642706). – Basil Bourque May 23 '18 at 02:10

1 Answers1

2

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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161