I'm facing a frustrating problem with static variables in an application I'm debugging (I did not code it myself).
I have a mysql database table called Transits. Transits has a string column (varchar) that holds a date (eventdt) of the format "yyyy/MM/dd HH:mm:ss".
In my corresponding hibernate class I have a Date variable that will store the value of eventdt. Thus when eventdt is read, the code converts it from a string to a Date.
The code uses SimpleDateFormat in order to do the conversion. The programmer used a static variable to hold the format, but unfortunately made a formatting error:
private static DateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
Leaving it as is, every date stored in the eventdt column that is on the noon hour gets stored in the corresponding Date variable as midnight. Obviously this is undesirable, so I attempted to fix it:
private static DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
However, the code continues to retain the previous format "yyyy/MM/dd hh:mm:ss".
I have done some reading up on static variables and did everything I could think of to clear it. I recompiled the code, then redeployed (on Glassfish 3.1.1). I restarted Netbeans (7.1.1) and then rebuilt/redeployed again. I cleared the Netbeans caches and repeated the same process. Then I deleted the entire Glassfish install, rebooted, recreated the domains, cleared the cache again, and went through the same rebuild/redeploy.
I also tried changing the code itself. I thought maybe it could be a threading issue, so I changed the variable to static final:
private static final DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Then I changed it so that the static final variable is the format string, and instantiated the DateFormat object in each method where it was needed:
// In class variable block
private static final String DATETIME_FORMAT_PATTERN = "yyyy/MM/dd HH:mm:ss";
// In class methods
DateFormat df = new SimpleDateFormat(DATETIME_FORMAT_PATTERN);
No matter what I've done, the same erroneous format "yyyy/MM/dd hh:mm:ss" is retained.
The only thing I can think of that I've NOT done yet, is remove and reinstall Netbeans. I've read somewhere that updating it helped too. Do I need to go that far in order to clear the old static variable?
I also haven't tried putting the code in a synchronized block or initializing it ThreadSafe (though I'm not even sure threading is the issue here...)
I would appreciate any guidance in the right direction, as I am running out of ideas. Thanks!
Edit: As requested here is code that uses the DateFormat. lpr is another Hibernate object consolidating Transits table and some other needed tables.
Date activityDate = null;
try {
activityDate = df.parse(getEventdt());
lpr.setActivityDateTime(activityDate);
} catch (Exception e) {
logger.warn("Unable to parse " + getEventdt() + " as a date.");
}