Zoran Trifunovski
Zoran Trifunovski

Reputation: 321

Parse date with SimpleDateFormatter

Here's the code that should work, but does not:

public static void main(String[] args) {
   String datata = "23:00:01 GMT, Sun Jul 28, 2012";
   String format = "HH:mm:ss zzz, EEE MMM dd, yyyy";

   try {
      DateFormat inputFormat = new SimpleDateFormat(format);
      Date parsedDate = inputFormat.parse(datata);
      System.out.println(parsedDate.toGMTString());
   } catch (Exception e) {
      e.printStackTrace();
   }
}

I'm getting a parse exception. I triple checked the patterns, I even wrote it one beneath the other, and I still get an exception. Help, anyone?

Upvotes: 0

Views: 163

Answers (1)

assylias
assylias

Reputation: 328568

It is probably because your default locale is not in english and the parser does not understand "Sun" and/or "Jul". Try using:

DateFormat inputFormat = new SimpleDateFormat(format, Locale.ENGLISH);

Upvotes: 4

Related Questions