This discussion is archived
8 Replies Latest reply: Mar 16, 2013 4:04 PM by jschellSomeoneStoleMyAlias RSS

Java 7 SimpleDateFormat "k" pattern parsing

801327 Newbie
Currently Being Moderated
Iit appears as though the "k" pattern parsing of SimpleDateFormat has changed for Java 7 (from Java 6).

The "k" pattern is for hours 1-24. In Java 6, a value of 0 is treated the same as a value of 24. Other values outside of range result in a null date. In Java 7, a value of 0 is no longer allowed, and anything outside of 1-24 results in a null date. Anyone else able to confirm this?

I guess you could call this a bug fix, as 0 really shouldn't be valid. But it's something to keep in mind for systems still on Java 6, as a lot of places in real life are sloppy about this, and I wouldn't be surprised to hear of people suddenly getting NPEs after upgrading to Java 7.
  • 1. Re: Java 7 SimpleDateFormat "k" pattern parsing
    PhHein Guru Moderator
    Currently Being Moderated
    No, they behave absolutely the same way for me. I reckon you've setLenient(false) when you've tried with Java 7. A "0" for the k pattern is only parsed when the formatter is lenient.
  • 2. Re: Java 7 SimpleDateFormat "k" pattern parsing
    801327 Newbie
    Currently Being Moderated
    I felt like maybe I was missing a piece of the puzzle here. That's one reason I posted. Thanks. I'll double check to see if different code was used on one versus the other.
  • 3. Re: Java 7 SimpleDateFormat "k" pattern parsing
    801327 Newbie
    Currently Being Moderated
    Looking back at the code, I see we are actually using setLenient(false) on the SimpleDateFormat, but not just in the Java 7 case.

    The javadocs for setLenient say this:
    "Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format."

    So it may be that this only manifests when you have setLenient(false) set. In such case, maybe it should reject 0, but at the moment, it appears not to in Java 6. Still investigating...
  • 4. Re: Java 7 SimpleDateFormat "k" pattern parsing
    PhHein Guru Moderator
    Currently Being Moderated
    Just try this snippet in 6 and 7 once with setLenient commented out and with the line. The behaviour is the same.
    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy k:mm:ss");
    //sdf.setLenient(false);
    try {
         Date d = sdf.parse("13.03.2013 0:15:15");
         System.out.println(sdf.format(d));
    } catch (ParseException e) {
         e.printStackTrace();
    }
  • 5. Re: Java 7 SimpleDateFormat "k" pattern parsing
    801327 Newbie
    Currently Being Moderated
    In Java 6, it prints the date either way. In Java 7, it prints the date without setLenient(false), but throws a ParseException with setLenient(false).

    Java 6 versions tested:
    1.6.0_25-b06
    1.6.0_41-b02

    Java 7 version:
    1.7.0_17-b02

    If you tried this with setLenient(false) in Java 6 and it threw a parse exception, could you post what version of Java 6 you were using?

    Might be interesting to also test out "H" pattern parsing, but I haven't done that yet.

    In my case, I was somewhat concerned about what new variations would suddenly be allowed through if I start using lenient parsing. At the same time, I know I need to allow the 0 hour and the 24 hour based on the data that has to be handled. Was thinking of just manually checking for 0 and changing it to 24 before parsing. I guess the other options are to start allowing lenient parsing or fix the incoming data stream.

    Edited by: Skotty on Mar 14, 2013 5:24 PM -- added inquiry as to what version of Java 6 PhHein tried, if any
  • 6. Re: Java 7 SimpleDateFormat "k" pattern parsing
    801327 Newbie
    Currently Being Moderated
    We dug into the Java source and found a new snippet of code that validates the number range in Java 7 that wasn't there in Java 6. This was probably viewed as a bug fix. It just happened to be a bug that our code was relying on. At the moment, it looks like we are just going to try to clean up the incoming data so it doesn't have both 0s and 24s, and leave lenient set as false.

    Thanks for the assistance.
  • 7. Re: Java 7 SimpleDateFormat "k" pattern parsing
    gimbal2 Guru
    Currently Being Moderated
    Skotty wrote:
    We dug into the Java source and found a new snippet of code that validates the number range in Java 7 that wasn't there in Java 6. This was probably viewed as a bug fix. It just happened to be a bug that our code was relying on. At the moment, it looks like we are just going to try to clean up the incoming data so it doesn't have both 0s and 24s, and leave lenient set as false.

    Thanks for the assistance.
    Ugh, fixing data problems in code in stead of making sure you don't get broken data in the first place. Those kind of hot-fixes are actually boils that eventually are going to pop and leave a mess. But you'll probably have to experience that at least once before you start to put your foot down and say no to these kind of garbage-fix-em-uppers.
  • 8. Re: Java 7 SimpleDateFormat "k" pattern parsing
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    gimbal2 wrote:
    Ugh, fixing data problems in code in stead of making sure you don't get broken data in the first place.
    That however is an assumption - that it is broken.

    See the following link and search for "between 00 and 24" and note the comment there.

    http://en.wikipedia.org/wiki/ISO_8601

    Further note that this is a description of a standard.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points