Java Dates from a n00b point of view

After spotting that seemingly 90% of the methods on the Date class were deprecated, I started getting worried.  Then when new Date().getYear() started giving me odd numbers like 109 instead of 2009.  I started thinking “Wow! Those Sun developers must have been on some strong drugs when they designed this language!”

So a bit more googling and some “WTF?!?!!?”’s later I found out that the Date class returns the time since January 1, 1970.  So of course .getYear() returns the number of years since then.  Well, not exactly.  Because there are only 39 years since 1 Jan 1970 and today.  So how the heck did getYear() return 109?  I didn’t bother to go any further than that – clearly the Sun developers were on drugs that should never have been invented, and I didn’t want to even think about starting to try and understand what was going on in their twisted little minds at that point in time.

BUT, I found out that they have a GregorianCalendar class which can help you out in most situations, even if it’s a little obtuse in how you use it.

The following code:

Calendar cal = new GregorianCalendar(mYear, mMonth, mDay, mHour, mMinute, mSecond);

Gives you a calendar object based on the input you gave it.  Now of course I get a Date object from a database so I want to do new GregorianCalendar(mDate).  But again, that would be asking too much, so instead of that simple instantiation I have to do:

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(creationDate);

Why, on earth did they call it setTime?  I mean, I’m setting a date and time.  I’m not just working with Time here folks! Surely the method should be named something a little better like .setDateTime() or .setDate()?

But wait… it gets better.  You know the Date class had a getYear() method, right? Remember? It’s the one that requires large amounts of hallucinogens to understand or use!  Well, naturally you’d expect that the Calendar class would have a getYear() that makes sense.

That’s where you’d be wrong!  Since they started with the obscure requirement of doing .GetInstance() followed by .setTime(mDate), they clearly hadn’t quite yet gotten off their high when they made the get() method.  Here’s how you use it:

Calendar cal = GregorianCalendar.getInstance();
cal.setTime(creationDate);
mYear = cal.get(GregorianCalendar.YEAR);
mMonth = cal.get(GregorianCalendar.MONTH);
mDay = cal.get(GregorianCalendar.DAY_OF_MONTH);

Yup, that’s right… why make it as simple as cal.getYear() when you can make it cal.get(GregorianCalendar.YEAR)?  Perhaps someone over at Sun had some stock in a company that provides treatment for RSI’s, so that by making us type out loads more letters they hoped to make more of us suffer not only the pain of using their language, but also of repetitive stress injuries.

BIG DISCLAIMER:

From my brief work with Java previously, I know that the designers were NOT on any drugs and in fact they probably had very good reason for designing the classes the way that they did. It’s just not immediately obvious to the new user, and that’s mainly what I’m ranting about here.  So, to all the Java fanboys out there who are preparing to unleash their full fury on me for my sacrilegious comments about their language – please take a moment and laugh before you post. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *