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