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);
        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,
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: Logo

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

Blog Stats

  • 247,852 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

%d bloggers like this: