Archive for October, 2011

Java is my nightmare

Java is my nightmare

“I will just add a date formatted string for Christmas this year,” I thought.  How hard could that be?  Much later, this code:

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar gcdate = new GregorianCalendar(2011, 12, 25);
        java.util.Date date = new Date();
        gcdate.set(GregorianCalendar.ERA, GregorianCalendar.AD);
        date.setYear(2011);
        date.setMonth(12);
        date.setDate(25);
        Log.d(TAG, "GCdate = "+gcdate.toString());
        Log.d(TAG, "date = "+dateFormat.format(date));
        long millis =  gcdate.getTimeInMillis();
        date = new Date( millis);
        Log.d(TAG, "date now is "+dateFormat.format(date));

Gives these log entries:

GCdate = java.util.GregorianCalendar[time=?,areFieldsSet=false,lenient=true,
zone=org.apache.harmony.luni.internal.util.ZoneInfo@310bdfd6,firstDayOfWeek=1,
minimalDaysInFirstWeek=1,ERA==1,YEAR==2011,MONTH==12,WEEK_OF_YEAR==?,
WEEK_OF_MONTH==?,DAY_OF_MONTH==25,DAY_OF_YEAR==?,DAY_OF_WEEK==?,
DAY_OF_WEEK_IN_MONTH==?,AM_PM==?,HOUR==?,HOUR_OF_DAY=?,MINUTE==?,
SECOND==?,MILLISECOND==?,ZONE_OFFSET==?,DST_OFFSET==?]
date = 3912-01-25 // ?!
date now is 2012-01-25 // What a Terrible Failure.

Obviously, I could just use “2011-12-25” and be done with it, but I will be working with other dates and need to understand why this lunacy. Surely any sane implementation would allow you to set the year, month and date using numbers? How hard can it be to set a Date object to a specific date??

Time and again I have this “how hard could it be?” experience with java ending up being something of a soul destroying experience.

The answer, by the way, appears to be that GregorianCalendar runs from 1-12, but Calendar, from which GregorianCalendar is subclassed, runs from 0-11 for the months.  If lenient is true, then values which are out of bounds are rolled into the following month etc.

Disposable Society (Printers)

Disposable Society (Printers)

Hmmmm….

I could either pay $500 for a new fuser for my (not so) old (maybe 4 or 5 years) HP printer or I could pay a little less and buy a complete, largely equivalent new laser printer.


Blog Stats

  • 226,407 hits

OSWALD Newsletter

If you would like to receive OSWALD, a weekly open source news digest please send an email to oswald (with the subject "subscribe") at opensourcelaw.biz