Tuesday, May 24, 2005

The Calendar Quiz

Given the code snippet below, what would you expect the hour variable is set to?
Calendar calendar = Calendar.getInstance();
calendar.clear(Calendar.HOUR_OF_DAY);
calendar.clear(Calendar.MINUTE);
calendar.clear(Calendar.SECOND);
calendar.clear(Calendar.MILLISECOND);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
Select an answer:
  1. 0
  2. null
  3. it depends
The right answer is the last one. The clear(int field) method does not change the value of the calendar's hour field. The documentation says: The HOUR_OF_DAY, HOUR and AM_PM fields are handled independently and the the resolution rule for the time of day is applied. Clearing one of the fields doesn't reset the hour of day value of this Calendar. Use set(Calendar.HOUR_OF_DAY, 0) to reset the hour value..

If only I knew it before :-)

Wednesday, May 04, 2005

Unable to find a javac compiler

Have you ever seen that nasty message? I have spent hours trying to figure out what's wrong with my build script (actually, I have dozens of javacs on my computer :-))
BUILD FAILED: D:\workspace\spschema\build.xml:37: 
Unable to find a javac compiler;
com.sun.tools.javac.Main is not on the classpath.
Perhaps JAVA_HOME does not point to the JDK
After a few google searches, after I almost finished reading the ant manual :-), I found this workaround at www.manning-sandbox.com: [Originally posted by burnette] The fix that works for me is to locate tools.jar (it's somewhere in the jdk directory) and add it to your Ant classpath (Window > Preferences > Ant > Runtime > Classpath). Make sure you haven't overriden this global setting for your particular ant xml file. Thanks, burnette!