• dpm (unregistered)

    If this wasn't in Java, I would swear it was code I wrote in pre-ANSI "C" back in 1984 when I was the rawest, greenest n00b to ever enter the programming workforce. I kid you not, the logic and tests look exactly like my beloved datetime.c file.

  • MiserableOldGit (unregistered) in reply to dpm

    Maybe that same code has just been ported forward with minor adaptations? I reckon a lot of the WTFs on here have a provenance like that.

    Still makes most of them WTFs, just possibly less so at the time they were written.

  • anonymous (unregistered)

    Didn't we had this one before a while ago? That checking function that takes a name parameter that's only used in error messages seems familiar.

  • my name is missing (unregistered)

    I am reminded of the monkey that eats a date in Indiana Jones and dies: "Bad dates".

  • WTFGuy (unregistered)

    I agree w anonymous. Some time in the last couple of months we saw the java date validator that throws IOException with no further details on what's wrong. Except on null which it silently ignores.

    I wonder if Remy got the same submission from two different people at the same IniTech. Really bad legacy code tends to belong to large organizations.

  • (nodebb) in reply to WTFGuy

    Two different people at the same IniTech. Both of them named "Cid".

  • MiserableOldGit (unregistered) in reply to anonymous

    Yep, looks like the same stuff, but a different focus.


    Could be Remy is reusing a mail in, or maybe this was some "answer" provided on an online forum or in a particularly crummy training book. Copypasta WTF

  • Prime Mover (unregistered)

    I was just about managing until I saw "intervall".

    Now I find that the effect of stomach acid on a laptop causes it to become unusable.

    I have had to type this using a different machine.

  • Sole Purpose Of Visit (unregistered)

    Not much use if you don't want the Gregorian calendar, is it?

    If you're going to clean things up, at least put a modicum of thought into it. Use a (defaulted) calendar as a parameter to the function, for the love of Pope.

  • WTFs by the Dozen (unregistered)

    Custom date/string munging isn't really that much of a WTF in Java - java.util.Date is famously bad and while there are lots of attempts to fix it since, none have really won, and the 'correct' way of doing it with SimpleDateFormat is kinda WTFy too.

    But using the string as the primary date format and doing arithmetic on the strings? Yeah, that's a bona fide WTF.

  • Chris (unregistered)

    The method for adding seconds will get tripped up with daylight savings and leap seconds (although I still have no idea how the latter works).

    What's the expected behaviour to adding a "day" to a datetime set to a few hours before daylight savings kicks in? The same hour on the next day, or 24 hours difference?

  • RLB (unregistered) in reply to Sole Purpose Of Visit

    calendar.settype(Calendar.ShireReckoning) ?

  • Just Me (unregistered)

    java.util.Calendar has been around since Java 1.1. It contains methods for adding an interval. It is unfortunate that Calendar.JANUARY is defined as 0. One gotcha with java.util.Calendar (and java.util.Date) is the timezone.

  • hwertz (unregistered)

    Gross. Even unaware of (as Just Me points out) the calendar interval methods, you'd think at least they'd be aware of unix timestamps. Of course, then instead of "adding 10 seconds" running that addseconds() method, it'd probably end up having a date string, having an equally gross bit of code manually convert the date string into a unix timestamp, add the seconds, then have a second gross block of code (with leap years etc., rather then calendar...) turning the timestamp back into a date string.

    Oh well.

  • (nodebb)

    TRWTF is using StringBuffer in any code written from Java 5 onwards (unless you're doing something seriously screwy with threads) instead of the considerably-faster StringBuilder. It's not even difficult to upgrade from one to the other.

  • Tim (unregistered)

    One thing that few date libraries handle properly (which may be one of the motivations for this code) is the concept of "date without time", which is a completely different thing from a date based on a timestamp.

    For example, consider 2 people born at the exact same millisecond but on opposite sides of the world. Their birthdays will be different and even if they go on holiday together, their passports will show them as being born on different days. if they are on holiday during their birthday they will celebrate them on separate days, regardless of wherever they are in the world. Storing and manipulating this using a simple string is often easier than trying to use a timestamp-based date system.

  • (nodebb) in reply to Chris

    Maybe this system doesn't believe in daylight saving?

    (A less likely scenario is that these are actually supposed to be UTC, so there's nothing to do here.)

  • Chuck (unregistered) in reply to WTFs by the Dozen

    The actually correct way is to use JodaTime. I'm surprised nobody has mentioned it yet -- the new date time APIs in Java are even based on it.

Leave a comment on “Dates by the Dozen”

Log In or post as a guest

Replying to comment #:

« Return to Article