1

I read a lot of advices on internet and StackOverFlow, but still I'm not understanding why I'm getting this issue on this Date parse. can someone help?

My code:

String date = " Aug 24 18:29:51 2017 GMT ";
System.out.println("@"+date+"@");
DateFormat format = new SimpleDateFormat(" MMM dd HH:mm:ss yyyy z ");
Date data = format.parse(date);
System.out.println("#"+data+"#");

Output:

@ Aug 24 18:29:51 2017 GMT @
java.text.ParseException: Unparseable date: " Aug 24 18:29:51 2017 GMT "
at java.text.DateFormat.parse(DateFormat.java:366)
...
...
Matt Vegas
  • 453
  • 5
  • 17
  • 1
    I believe you'll find an answer to your question at [this stack](https://stackoverflow.com/questions/19112357/java-simpledateformatyyyy-mm-ddthhmmssz-gives-timezone-as-ist) – avi.elkharrat Jun 26 '18 at 08:06
  • 1
    Are you sure this is a good representation of the code you're actually running? This snippet works just fine on my machine. – Mureinik Jun 26 '18 at 08:07
  • This code is working fine, could you please check your imports java.util.Date etc – ajay tomar Jun 26 '18 at 08:31
  • 1
    If you’ve read a lot, then you have also read that you should avoid the `SimpleDateFormat` class. It is not only long outdated, it is also notoriously troublesome. Today we have so much better in [`java.time`, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jun 26 '18 at 08:48

2 Answers2

3

It seems that your default Locale doesn't cope with the english word for the month of August.

You may want to force the English Locale :

DateFormat format = new SimpleDateFormat(" MMM dd HH:mm:ss yyyy z ", Locale.ENGLISH);
Arnaud
  • 17,229
  • 3
  • 31
  • 44
  • I don't have another explanation myself, but it does seem very weird that OP's locale cannot parse a date that it formatted itself. – kumesana Jun 26 '18 at 08:18
  • @kumesana - how do you conclude that from the question? – rustyx Jun 26 '18 at 08:19
  • @rustyx by misreading it. For some reason I thought at one moment the code formatted the date with the month as 'Aug'. I was wrong. – kumesana Jun 26 '18 at 08:21
  • yes adding this Locale.ENGLISH i fixed, but why is so relevant? thanks @ Aug 24 18:29:51 2017 GMT @ #Thu Aug 24 20:29:51 CEST 2017# – Matt Vegas Jun 26 '18 at 08:25
  • 1
    @MattVegas What do you mean why is it relevant? In French, August is written Août. As you can see, a 'g' has no reason to be there when wanting to describe August in this language. So there are languages where 'Aug' just cannot be parsed as August, nor as anything at all. That should feel obvious. – kumesana Jun 26 '18 at 08:30
  • 1
    @MattVegas : That's just the timezone that `Date.toString()` displays (see https://stackoverflow.com/questions/4199217/what-time-zone-does-date-tostring-display), there is nothing wrong with the value, but you may want to create another `DateFormat` to format you `Date` to the output you want. – Arnaud Jun 26 '18 at 08:35
1

Can you check your locale using Locale.getDefault(). You might have to set this to english.

sjaymj62
  • 386
  • 2
  • 18