0

I am trying to parse the following date/time:

Wed, 29 Aug 2018 12:56:00 +0200

Currently, I am using the following code which works on my Android emulator but on my actual phone I get a "java.text.ParseException: Unparseable date"

SimpleDateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
this.pubDate = sdf.parse(s_pubDate);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
IchBestäube
  • 61
  • 3
  • 11
  • Try with other date format like "dd MMM yyyy". – Ankit Patidar Aug 29 '18 at 12:20
  • This works, but what do I need to change in my pattern then? – IchBestäube Aug 29 '18 at 12:23
  • I will try for you, please provide me any value passing to variable "s_pubDate" – Ankit Patidar Aug 29 '18 at 12:26
  • 1
    As an aside consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends, and adding [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project in order to use `java.time`, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Aug 29 '18 at 13:38
  • 2
    Possible duplicate of [Java - Unparseable date](https://stackoverflow.com/questions/6154772/java-unparseable-date) and/or of [Getting error java.text.ParseException: Unparseable date: (at offset 0) even if the Simple date format and string value are identical](https://stackoverflow.com/questions/46285384/getting-error-java-text-parseexception-unparseable-date-at-offset-0-even-if) – Ole V.V. Aug 29 '18 at 13:39
  • Search Stack Overflow before posting. This topic has been asked and answered many times already. – Basil Bourque Aug 29 '18 at 15:46

2 Answers2

3

You need to explicitly set the formatter locale, otherwise it would try to pars it based on phone locale and that may cause an error. I think that the cause in your case.

Your emulated device has one locale, while your phone has the one, that can't pars date in such format.

SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z",
                    **YOUR DESIRED LOCALE**);
Taier
  • 2,109
  • 12
  • 22
  • I don't think that's the issue. Because local is just changed the date respective to that country not change its format. – Ankit Patidar Aug 29 '18 at 12:25
  • 1
    @ankitpatidar actually, it does, for example, when parsing a date, depending on a country it would look for "." as a separator, not "," or otherwise and etc. – Taier Aug 29 '18 at 12:32
  • 1
    Yes i think you are right. – Ankit Patidar Aug 29 '18 at 12:37
  • I also don't really understand why but it worked on my phone when I use the local US. My phone is on german and the rss feed I am parsing this date from is also german. A bit confusing though.. – IchBestäube Aug 29 '18 at 12:38
  • @IchBestäube apparently they use US formatter as a standard (a common thing, also it is a default one :D). Anyway, now you know more :) – Taier Aug 29 '18 at 12:41
  • @IchBestäube Your `SimpleDateFormat` with German locale would expect `Mi.` for Mittwoch (Wednesday) and cannot parse `Wed` (no matter if it comes from a German RSS feed; as I said in my answer, RFC 1123 format is always in English). Therefore you got the parse exception. If your JVM has US locale setting, `Wed` is expected and parsed without a problem. – Ole V.V. Aug 29 '18 at 16:18
1

java.time

    String dateTimeString = "Wed, 29 Aug 2018 12:56:00 +0200";
    OffsetDateTime dateTime = OffsetDateTime.parse(
            dateTimeString, DateTimeFormatter.RFC_1123_DATE_TIME);
    System.out.println(dateTime);

Output from this snippet is:

2018-08-29T12:56+02:00

The format of the string you want to parse is RFC 1123. This format is built into Java, so spares you the trouble of building your own formatter for parsing. Furthermore RFC 1123 is always in English, so there is no risk that your phone’s default locale (or any other locale) interferes.

I am using java.time, the modern Java date and time API. The SimpleDateFormat that you tried to use is not only long outdated, it is also notoriously troublesome.

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