Why SimpleDateFormat HH works differently from hh

    final String sourceDate = "05.12.2014 12:17";
    final String testDate = "05.12.2014 13:17";
    final SimpleDateFormat originalSDF = new SimpleDateFormat("dd.MM.yyyy hh:mm");
    final SimpleDateFormat testSDF = new SimpleDateFormat("dd.MM.yyyy HH:mm");

    System.out.println(originalSDF.parse(sourceDate).toString());
    System.out.println(testSDF.parse(sourceDate).toString());
    System.out.println("=====================");
    System.out.println(originalSDF.parse(testDate).toString());
    System.out.println(testSDF.parse(testDate).toString());

The output will is:

Fri Dec 05 00:17:00 GMT 2014
Fri Dec 05 12:17:00 GMT 2014
=====================
Fri Dec 05 13:17:00 GMT 2014
Fri Dec 05 13:17:00 GMT 2014

Why with all time this formats works same, but with 12:* it works different, hh:mm parse as 12h format and HH:mm parse as 24h format?

prooflink

Jon Skeet
people
quotationmark

If you use originalSDF.setLenient(false) then parsing "05.12.2014 13:17" will throw an exception... basically in lenient mode, hh is treated as HH (i.e. as a 24-hour value) when the value is greater than 12 (and possibly when it's 0).

Personally I think that defaulting to lenient mode is a bad idea, but that's a different matter... basically your format should always be appropriate for your data. If you're going to get a value of 13:17, you should be using HH:mm.

people

See more on this question at Stackoverflow