2

The published date String is returned as : Sun, 18 Nov 2012 06:50:02 GMT and my method to convertDate is returning me java.text.ParseException: Unparseable date: "Sun, 18 Nov 2012 06:50:02 GMT" (at offset 8). I checked resources but couldn't find them much helpful like this one ... Parsing a String with a GMT timezone to Date using SimpleDateFormat

Would really appreciate if you can shed some light on it. Here is my method .. pubDate here is Sun, 18 Nov 2012 06:50:02 GMT

public Date convertDate(String pubDate){

    SimpleDateFormat sdf =  new SimpleDateFormat("EEE, dd MM yyyy hh:mm:ss Z",Locale.US);
    Date newsDate = new GregorianCalendar(0, 0, 0).getTime();
        try{
            newsDate  = sdf.parse(pubDate);
        }catch(ParseException e){
            Log.d(Tag, "Exception Parsing date" + pubDate);
        }
    return null;
}
Community
  • 1
  • 1
Tameem Ahmed
  • 71
  • 1
  • 2
  • 7

2 Answers2

5

Use MMM for the month field:

new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss Z",Locale.US);

Once this is fixed, don't forget to return newsDate as you're currently returning null.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Here is a little test I did:

@Test
public void testDate() throws ParseException {

    String pubDate = "Sun, 18 Nov 2012 06:50:02 GMT";

    SimpleDateFormat sdf = new SimpleDateFormat(
            "EEE, dd MMM yyyy hh:mm:ss z", Locale.US);
    Date newsDate = new GregorianCalendar(0, 0, 0).getTime();

    newsDate = sdf.parse(pubDate);

}

You need to set the month with MMM because you are using the first three letters of the month (and four M would mean the full month name). Also, the timezone must be z instead of Z because you have a ISO 8601 Time zone (GMT).

ElderMael
  • 7,000
  • 5
  • 34
  • 53