• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange behavior of the Calendar get() method  RSS feed

 
Vipul
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange behavior of the Calendar get() method

I have follwing code involving use of get() method of the calendar object .

The problem is that the date is getting offset by 14 days back (every time)when i try to use these method to set the date on a object and this happens for year earlier then 1970.

The strange part is that this happens on the prod server and not on my local server .( i am using websphere 5.1 nad IDE:WSAD ,JDK 1.4)

I have checked all the settings on the servers ...(locale etc ) they are identical ...


here is the code
=====================================================================
//i am trying to move date from aDriver to aDriverInfo object
//aDriver.getBirthDt() returns a calendar

if (aDriver.getBirthDt() != null) {

aDriverInfo.setDobYYYY(DateUtil.getYear(aDriver.getBirthDt()));
aDriverInfo.setDobMM(DateUtil.getMonth(aDriver.getBirthDt()));
aDriverInfo.setDobDD(DateUtil.getDay(aDriver.getBirthDt()));
}
//the yy mm dd on the aDriverInfo gets offset by 14 days (for year before 1970)
=================================================================
DateUtil class

public static String getYear(Calendar aCalendar){
String myYear = null;
if (aCalendar != null){
myYear = Integer.toString(aCalendar.get(Calendar.YEAR));
}
return myYear;
}

=============
public static String getMonth(Calendar aCalendar) {
String myMonth = null;
if (aCalendar != null) {
int myMonthValue = aCalendar.get(Calendar.MONTH);
myMonth = Integer.toString(myMonthValue + 1);
myMonth = StringUtils.leftPad(myMonth, 2, "0");
}
return myMonth;
}

==============
public static String getDay(Calendar aCalendar) {
String myDay = null;
if (aCalendar != null) {
myDay = Integer.toString(aCalendar.get(Calendar.DATE));
myDay = StringUtils.leftPad(myDay, 2, "0");
}
return myDay;
}
===============================================================


Any help on this issue us appreciated !!!
 
Paul Clapham
Sheriff
Posts: 22697
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So why the unlikely choice of Calendar.get() as the guilty party? As opposed to, say, the code that creates the Driver object?
 
Vipul
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick reply ...

If I use the same driver object in the following way ....all works fine ..


if (aDriver.getBirthDt() != null) {
String birthDate = DateUtil.getStringFormatMMddYYYYWithHyphens(aDriver.getBirthDt());

aDriverInfo.setDobDD(birthDate.substring(3,5));
aDriverInfo.setDobMM(birthDate.substring(0,2));
aDriverInfo.setDobYYYY(birthDate.substring(6));
}


==========
public static String getStringFormatMMddYYYYWithHyphens(Calendar aCalendar) {

SimpleDateFormat myFormatter = new SimpleDateFormat("MM-dd-yyyy");

if (aCalendar != null) {
Date aDate = aCalendar.getTime();
String myFormattedDate = myFormatter.format(aDate);
return myFormattedDate;
}
return null;
}
=========================

I was suspecting the recomputization of the calendar object ...but that doens't explain why it works on the local server and not in prod ...

Recomputization of the calendar object
"set(f, value) changes field f to value. In addition, it sets an internal member variable to indicate that field f has been changed. Although field f is changed immediately, the calendar's milliseconds is not recomputed until the next call to get(), getTime(), or getTimeInMillis() is made. Thus, multiple calls to set() do not trigger multiple, unnecessary computations. As a result of changing a field using set(), other fields may also change, depending on the field, the field value, and the calendar system. In addition, get(f) will not necessarily return value after the fields have been recomputed. The specifics are determined by the concrete calendar class."

Thanks
[ August 04, 2006: Message edited by: vish pat ]
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might have already tried this but what I would do is basic debugging...

in your code,

aDriverInfo.setDobYYYY(DateUtil.getYear(aDriver.getBirthDt()));
aDriverInfo.setDobMM(DateUtil.getMonth(aDriver.getBirthDt()));
aDriverInfo.setDobDD(DateUtil.getDay(aDriver.getBirthDt()));

I would compare,

Calendar c1 = aDriver.getBirthDt();
int d1 = c1.get(Calendar.DATE);

String d2 = DateUtil.getDay(aDriver.getBirthDt());

if ( d2.equals(d1+"") ) {
System.out.println("I don't see anything wrong here."+d2+","+d1);
} else {
System.out.println("Yippie, found the problem!"+d2+","+d1);
}

I doubt the possible bug in the StringUtil.leftPad() ?? Just a wild guess...

Regards
Maulin
 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
aqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
 
Vipul
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the debugging ...its prints same date on the local server and diff dates on the prod/Dev server(WAS 5.1).

and i am using string utils from the org.apache.commons.lang.StringUtils...so thats should not be the culprit..

thanks
[ August 08, 2006: Message edited by: vish pat ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!