1

I'm trying to convert a date from String to a Date object:

String dateString = "Mon, 04 Sep 2017 18:30:28";
String dateFormat = "EEE, dd MMM yyyy HH:mm:ss";

Results in the following exception:

java.text.ParseException: Unparseable date: "Mon, 04 Sep 2017 18:30:28"

I tried different strings and formats and the problems seems to be the name of week ('EEE'). Without it works perfectly.

Also this works perfectly:

String dateString = "04 Sep 2017 18:30:28, Mon";
String dateFormat = "dd MMM yyyy HH:mm:ss, EEE";
Dampignak
  • 51
  • 6
  • 4
    The names are in English, so it could be related to the locale: https://stackoverflow.com/q/6154772/7605325 –  Sep 05 '17 at 13:40
  • 1
    Can you provide the actual code you wrote to parse the date? – Lothar Sep 05 '17 at 13:40
  • 2
    As an aside, I encourage you to skip the outdated classes `Date` and `SimpleDateFormat`. [The modern Java date and time API AKA `java.time` or JSR-310](https://docs.oracle.com/javase/tutorial/datetime/) is much nicer to work with. – Ole V.V. Sep 05 '17 at 14:02
  • @OleV.V. Thanks for the advice. I will have a look at that in future. But I'm working on a bigger project and it's not easy (in face it's pretty hard) to introduce new APIs/Librarys :-) – Dampignak Sep 05 '17 at 14:10
  • 2
    If using Java 8 or 9, the newer API is built in. Maybe you can start using it without anyone noticing. :-) To use it with Java 6 or 7, you will need [ThreeTen Backport](http://www.threeten.org/threetenbp/), which you probably cannot introduce without project approval (even though it will only be needed until you eventually upgrade to Java 8). – Ole V.V. Sep 05 '17 at 14:14
  • 1
    @Dampignak You can easily convert to/from the java.time types, so it is actually quite easy to integrate in new code on an existing project. For Java 8 and later, look to new conversion methods added to the old classes. In the back-port, look to a utility class for conversion methods. – Basil Bourque Sep 05 '17 at 16:24

2 Answers2

6

Month and weekdays are abbreviated text. The text is language dependent and if you don't provide a specific Locale when instantiating the SimpleDateFormatter the system's Locale is used instead. The reason why your parsing fails with the weekday and not the month can be that the abbreviated name of the month in your system's default language is coincidentally the same as in English.

Here is some code how you should parse a date with text:

SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
formatter.setLenient(false);
ParsePosition pos = new ParsePosition(0);
return formatter.parse(toParse, pos);

with toParse being a string containing your date as text.

Lothar
  • 5,323
  • 1
  • 11
  • 27
  • You are right, but I still don't understand why the second case (name of month + name of day) works but the first case not (name of day at the beginning) – Dampignak Sep 05 '17 at 13:55
  • @Dampignak Can't explain that, either. At least without knowing your system's default Locale (`System.out.println(Locale.getDefault());` should show it to you in case you don't know) – Lothar Sep 05 '17 at 13:59
  • My default locale is German (de_DE) – Dampignak Sep 05 '17 at 14:03
  • @Dampignak "Sep" is the same abbreviation in German and English. – Meno Hochschild Sep 05 '17 at 15:00
  • 1
    @MenoHochschild Monday and Montag, too. That's why it's strange a bit, I expected Dampignak to answer something like fr_FR. – Lothar Sep 05 '17 at 15:02
  • The standard abbreviation for Monday in German is not "Mon" but "Mo" in wall calendars, so here is a difference compared with English. – Meno Hochschild Sep 05 '17 at 15:23
  • 1
    That might explain why the second variant worked. Since the week day name is at the end, the check ends with reaching `Mo` and the `n` is ignored. The other template specifies, that after the name there is a comma, but after the evaluation of the week day, there is still an `n` left, so the template doesn't fit leading to the parsing error. – Lothar Sep 05 '17 at 15:54
  • @Lothar I think you're right. Just set the default locale to German and tested with `04 Sep 2017 18:30:28, Mowhatever` and it works. Once the pattern is completely parsed, the rest of the input is ignored. –  Sep 05 '17 at 16:23
0

You haven't added the locale which must have caused parse exception while parsing text(s) for Day Of Week & Month

    public static void main(String[] args) throws ParseException {
        String dateString = "Mon, 04 Sep 2017 18:30:28";
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss", Locale.ENGLISH);
        System.out.println(dateFormat.parse(dateString));
    }

Produces the following output

Mon Sep 04 18:30:28 IST 2017
Sridhar
  • 1,518
  • 14
  • 27
  • Yes, seems logic.. but why is the second case working and the first case not? Both have same fields but different order? – Dampignak Sep 05 '17 at 14:01