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.

2 Responses to “Java is my nightmare”


  1. 1 Anonymous 23 September 2014 at 8:31 am

    Well you just have to learn every single class ever written in java and keep abreast of it. So no problem then as long as you’re not human. Java sucks. OK I’m bitter.

  2. 2 Bob 7 July 2015 at 11:13 am

    I know this is an old post but it should be emphasized how unreliable and quirky GregorianCalendar is compared to everything else in Java. Java’s great but I really hate GregorianCalendar, it often isn’t intuitive. good thing Java 8 is replacing it with something better


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




Blog Stats

  • 219,507 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

%d bloggers like this: