-1

I'm tryint to transform "Wed, 22 Apr 2015 05:45:42 GMT" to "yyyy MM dd - HH:mm"

But that string does not fit in all these simpledateformats, it gives exception in all three

new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'Z'");
NullPointerException
  • 36,107
  • 79
  • 222
  • 382

2 Answers2

2

I can reproduce your problem since I am sitting in a non-english-speaking country, too. The solution is to specify English as language because your input contains an English word/abbreviation (look at the day of week "Wed"!). Following solution works:

SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH);
Date d = sdf.parse("Wed, 22 Apr 2015 05:45:42 GMT");
SimpleDateFormat out = new SimpleDateFormat("yyyy MM dd - HH:mm", Locale.ROOT);
System.out.println(out.format(d)); // output in the system timezone

It does not matter what your app is for (worldwide or not). The only fact which matters is the language of your text input. If this is varying in language then and only then you have first to determine the language of the device in question (probably Locale.getDefault()) and set it on your SimpleDateFormat-object.

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
0

Use small z in date format

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");

new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss 'z'");

I have checked it with below code, it is working.

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
    try {
        Date date =simpleDateFormat.parse("Wed, 22 Apr 2015 05:45:42 GMT");
        Log.e(MainActivity.class.getSimpleName(),"date : "+date.toString());
    } catch (ParseException e) {
        e.printStackTrace();
    }
Karn Shah
  • 501
  • 4
  • 14