- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
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.
Admin
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.
Admin
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.
Admin
I am reminded of the monkey that eats a date in Indiana Jones and dies: "Bad dates".
Admin
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.
Admin
Two different people at the same IniTech. Both of them named "Cid".
Admin
Yep, looks like the same stuff, but a different focus.
https://thedailywtf.com/articles/going-on-an-exceptional-date
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
Admin
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.
Admin
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.
Admin
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.
Admin
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?
Admin
calendar.settype(Calendar.ShireReckoning) ?
Admin
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.
Admin
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.
Admin
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-fasterStringBuilder
. It's not even difficult to upgrade from one to the other.Admin
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.
Admin
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.)
Admin
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.