SimpleDateFormat: unexpected results and unexpected parse exceptions

I'm having huge difficulties with simple date format. First of all, I know not all tutorials on all sites are actually good, but the fact that all non trivial formats (not dd/MM/yyyy) gave a parse exception (plus my own tests don't work as expected) is rather frustrating to me.

This is the site in question: http://www.mkyong.com/java/how-to-convert-string-to-date-java/

And I don't understand why something as simple as:

private static void unexpectedFailure() {
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
    String dateInString = "7-Jun-2013";

    try {

        Date date = formatter.parse(dateInString);
        System.out.println(date);
        System.out.println(formatter.format(date));

    } catch (ParseException e) {
        e.printStackTrace();
    }
}

Throws a parse exception.

Also besides that, I'm trying to parse my own dates. This code gives strange results (unexpected I would say):

public static void doSomething(List<String> list) { 
    Iterator<String> iter = list.iterator();
    String[] line = iter.next().split(" ");
    System.out.println(Arrays.toString(line));
    DateFormat format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
    format.setLenient(true);


    try {
        System.out.println(line[0]+" "+line[1]);
        System.out.println(format.parse(line[0]+" "+line[1]));
    } catch (ParseException e) {
        System.out.println("In theory this should not get caught.");
    }
}

Prints out this:

[06/08/2015, 13:51:29:849, DEBUG, etc...]
06/08/2015 13:51:29:849
Thu Aug 06 13:51:29 EEST 2015

Thu Aug 06 13:51:29 EEST 2015 WHAT? WHY?

EDIT I'll try and explain. In my last code snippet I'm just trying to determine if the string is a date, and it passes "the test". However when I'm printing it out the format is simply bizzare. I'm starting to think that is because I'm printing a date. How can I even print a DateFormat? What I was expecting was dd/MM/yyyy hh:mm:ss not ddd MMM 06? hh:mm:ss G YYYY

Jon Skeet
people
quotationmark

And I don't understand why something as simple as:
(code snipped)
Throws a parse exception.

My guess is that it's tripping up over Jun which may not be a valid month abbreviation in your system default locale. I suggest you specify the locale in your SimpleDateFormat constructor:

SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy", Locale.US);

Then you're dealing with a locale which definitely has Jun as a month abbreviation.

That said, where possible I'd suggest using numeric formats where possibly, ideally ones following ISO-8601, e.g.

yyyy-MM-dd'T'HH:mm:ss

However when I'm printing it out the format is simply bizzare.

No it's not. You're effectively using

Date date = format.parse(line[0]+" "+line[1]);
System.out.println(date);

So that's calling Date.toString(), documented as:

Converts this Date object to a String of the form:

dow mon dd hh:mm:ss zzz yyyy

So that's working as expected. However, you want to format the date using your SimpleDateFormat - so you need to call format:

System.out.println(format.format(date));

Of course that just checks that it can round-trip, basically.

As a side note, I suspect you want HH (24-hour clock) instead of hh (12-hour clock) in your format string.

people

See more on this question at Stackoverflow