Image

Imagefosterbass wrote in Imagejava_dev

Calendar.set(Calendar.YEAR) is destructive on aix

I just got bitten by a platform difference and wanted to share what I learned. In my current job, I write my java code using Windows and deploy it to AIX. This has worked for over a year.


Today I wasted six hours tracking down a bug, thinking it was data-related (the code imports JDEdwards data, alters it a but, then stores it in some Oracle tables. The problem turned out to be a difference in how AIX handles Calendar.set() and had nothing to do with the data.



<br />import java.io.*; <br />import java.util.Calendar;<br /><br />public class DateTest {<br /><br /> public static void main(String args[]) {<br /> <br /> if (args[0]==null || args [1]==null) {<br /> System.out.println("Usage DateTest 1994 338");<br /> System.exit(-1);<br /> }<br /> System.out.println("Creating Calendar to represent day number "+args[1]+" of "+args[0]);<br /> <br /> int theYear = Integer.parseInt(args[0]);<br /> int theDayOfYear = Integer.parseInt(args[1]);<br /> <br /> Calendar theCal= Calendar.getInstance();<br /> theCal.set(Calendar.YEAR,theYear);<br /> theCal.set(Calendar.DAY_OF_YEAR,theDayOf<wbr />Year);<br /> <br /> System.out.println("When Calendar.YEAR is set first, the following date is created: " + theCal.get(Calendar.MONTH) +<br /> "/" + theCal.get(Calendar.DATE) + <br /> "/" + theCal.get(Calendar.YEAR) );<br /> <br /> Calendar theOtherCal= Calendar.getInstance();<br /> theOtherCal.set(Calendar.DAY_OF_YEAR,the<wbr />DayOfYear);<br /> theOtherCal.set(Calendar.YEAR,theYear);<br /><br /> <br /> System.out.println("When Calendar.DAY_OF_YEAR is set first, the following date is created: " + theOtherCal.get(Calendar.MONTH) +<br /> "/" + theOtherCal.get(Calendar.DATE) + <br /> "/" + theOtherCal.get(Calendar.YEAR) ); <br /><br /> }<br />}<br />

Note: The two println statements returns the same date Windows, but different dates on AIX. The date returned by AIX is the current month and day, but the year is taken from the arg entered on the command line. It seems that setting the year causes the month and day to become 'today'.


The difference is that in the first case, Calendar.YEAR is set before Calendar.DAY_OF_YEAR and in the second case, Calendar.DAY_OF_YEAR is set before Calendar.YEAR.