Java has, shall we say, rather interesting date manipulation. If you want a 'sane' date object that accurately tracks dates, you need the 'GregorianCalendar' class.
Most classes, even GregorianCalendar itself, use the Date class extensively. Amusingly this class already contains all the information in Date. As an even more enjoyable kicker, both of these are timestamps with millisecond precision, and not really 'date' classes at all.
If we want to output today's date in a format like 2006-08-09:
Calendar today = new GregorianCalendar();
DateFormat formatter = new SimpleDateFormat("yyyyMMDD");
System.out.println( formatter.format( today.getTime() ) );
Call me crazy, but I expected a Calendar class to be able to do calendar stuff, like outputting sane values for dates. If you try a toString on a calendar class you get tons of unusable debugging garbage instead of a sane date. Try this tasty tidbit for a RFC 822 date:
DateFormat formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");
System.out.println( formatter.format( today.getTime() ) );
Would it have killed them to make a GregorianCalendar.rfc822string function? Even an RFC822DateFormat would be nice. As a non-expert on dates, I can't even be 100% certain my code is fully compliant.
You may be wondering, why use a GregorianCalendar instead of just making a Date directly? Because Dates are useless alone and it ends up being even more work. To subtract two days from a Date, for example:
Date date = new Date();
Calendar cal = new GregorianCalendar();
cal.setTime(todayDate);
cal.add(Calendar.DATE, -2);
date = cal.getTime();
Especially enjoyabe is the use of 'add' and a negative int to go backwards in time. No doubt it would have killed the developer's fingers to type up a simple wrapper function like cal.subtract to make things more intuitive.
There are plusses to all this complexity; once you learn how to do all these manipulations, you can basically do anything with a date. But it really shouldn't be this annoying to do simple things.
I was recently asked how to do custom date parsing in Perl, specifically for a date in the format mmddyyyy. I work with dates frequently in Java, so I am familiar with using SimpleDateFormat objects to parse dates in this manner. The Perl equivalent is Da
Tracked: Oct 25, 17:47