Skip to main content

The Gods Must Be Crazy - Java Edition

Posted by felipegaucho on July 23, 2007 at 12:23 PM PDT

This blog entry is the summary of a long discussion I had with my friend
Jason Brazile about
Calendar and Date manipulation in Java. Jason is one of those agnostics against Java platform
and he is also a high skilled developer and researcher, so our discussions often
unveil interesting details about the Java language. This one made me be totally stoned.

How many concrete implementations of the java.util.Calendar are released in your JDK?

  1. ONE: the Gregorian Calendar
  2. TWO: the Gregorian Calendar and a new one from Japan
  3. THREE: because there is secret Calendars somewhere
  4. None of the above

Well, I have to admit I am quite surprise with the answer but, like a Java Puzzler, the
most correct answer is: None of the above. If you ask this for a specific version of the JDK,
perhaps I can guess how many calendars you have available - but the generic answer considers it is dependent
of the JDK version, and probably also dependant on particular revisions of JDK nightly builds.

Skeptic? Just copy/paste and run the code below:

	public static void main(String[] args) {
// JDK 1.5: This is the hidden Buddhist bug
Calendar budish = Calendar.getInstance(new Locale("th", "TH"));

// JDK 1.6: This is the undocumented Imperial mistake
Calendar japanese = Calendar.getInstance(new Locale("ja", "JP", "JP"));

// Any JDK: nobody really knows about the type - suposedely Gregorian
Calendar suposeGregorian = Calendar.getInstance();

For the ones without time to play mind games, the output here in my machine was this one:
	class sun.util.BuddhistCalendar
class java.util.JapaneseImperialCalendar
class java.util.GregorianCalendar

In respect of my lovely technology I will avoid to transcript here my first thoughts about that, but I propose to the reader
the following questions:

  • What kind of Calendar is instantiated when you call Calendar.getInstance()?
    • Answer: I suppose it is GregorianCalendar, but if you are trying it in Thailand, who knows?
  • PersonalJava API and
    debug packages are supposed to be available in the production versions of JDK?

    • Answer: to allow an API to run an unpredicted behaviour based on an undocumented library
      seems weird. The Japanese Imperial Calendar seems less complicated because its package follows the recommended
      naming convention and it is just not documented. But the Buddhist Calendar should be the preferred Calendar of Mr. Val Valentino
  • If your system uses the Calendar.getInstance() method, is it safe? Even if some day it runs in a place
    containing a dangerous Locale? How easy is to find a bug if a developer is testing in a safe Locale and the
    production environment is in a dangerous one?

    • Answer: I have now answer for that ;) please help to enlight my mind on this topic.
  • Related Topics >>