0

A message comes from the server in this format:

String strginDate = "Fri, 05 Jan 2018 15:19:14 GMT";
GMTToDDMMMYYYY(String strginDate)

So, I parse it this way::

private String GMTToDDMMMYYYY(String gmt){
    DateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss z");
    sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd mmm yyyy", Locale.getDefault());
    Date d = null;
    try {
        d = sdf.parse(gmt);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    String dateStr = simpleDateFormat.format(d);
    return dateStr;
}

And I get the error:

java.text.ParseException: Unparseable date: "Fri, 05 Jan 2018 15:19:14 GMT" (at offset 0)

But I tried everything, like this:

DateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss 'GMT'");

or

DateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'GMT'");

and even so:

DateFormat sdf = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'");

But the result is the same.

Where am I doing wrong?

No Name
  • 741
  • 8
  • 18
  • Probably a locale problem. What is `Locale.getDefault()`? – Ole V.V. Jan 30 '18 at 09:23
  • 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. Furthermore the built-in `DateTimeFormatter.RFC_1123_DATE_TIME` matches the format from your server, so no need to fiddle with your own format pattern string. – Ole V.V. Jan 30 '18 at 09:24
  • [Locale should not be a problem ... rather whitespace in the string](https://ideone.com/hYPYab) ... your code is working fine check if there is no "invisible" character in your date string – Selvin Jan 30 '18 at 09:25
  • I also had a similar problem. Can you try this: new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US); – Rino Jan 30 '18 at 09:25
  • @Selvin Your ideone shows a parse exception similar to the one in the question for German locale when the string is in English. How can you say locale isn’t a problem? – Ole V.V. Jan 30 '18 at 09:31
  • Because first date is parsing(even with german locale) and second (with leading "\0") is not parsing and \0 is not visible in console(logcat) ... but maybe Android's implementation is more strict... – Selvin Jan 30 '18 at 09:33
  • Just FYI, even if you parse it m stands for Minute in hour and M stands for Month in year..so consider changing it as it will not show 01 for Jan as you are expecting – Akshay Jan 30 '18 at 09:33
  • 2
    @Rino Locale.US Helped me. Thanks!!! – No Name Jan 30 '18 at 09:36
  • I tried Locale.getDefault() and it didn't work. But your answer helped. Thanks again – No Name Jan 30 '18 at 09:50
  • @Selvin, when I run your ideone code locally on my Java 9, I get `Unparseable date: "Fri, 05 Jan 2018 15:19:14 GMT"` already from the first string, the one without the null character. If I change the string to begin with `Fr., 05`, it works with the German locale (German for Friday is “Freitag”, abbreviated “Fr.” with a dot). – Ole V.V. Jan 30 '18 at 10:55
  • 1
    Possible duplicate of [How to convert String time stamp to java.sql.Timestamp?](https://stackoverflow.com/questions/43276767/how-to-convert-string-time-stamp-to-java-sql-timestamp). Or [Android converting date time parse error (even tried joda time)](https://stackoverflow.com/questions/44487282/android-converting-date-time-parse-error-even-tried-joda-time). Use your search engine to find more similar questions and answers. They are out there. – Ole V.V. Jan 30 '18 at 11:00

0 Answers0