java date + calendar problem

Hello to All,

I'm developing a server-side application has a part, which counts users expiry.

I'm using the following method to count the days between the expiry date (toSubtract) and the current date (returned by getStartDate()):

/**

* Subtract a date of a given Date from the start date.

* Returns number of days. It is positive if date is due.

*/

public int subtractDate(Date toSubtract) throws FrwException {

TRACER.traceEnter("TimeAccess.subtractDate(", toSubtract);

Calendar s = new GregorianCalendar();

TimeZone z = TimeZone.getTimeZone("UTC");

s.setTime(getStartDate());

int y1 = s.get(Calendar.YEAR);

int m1 = s.get(Calendar.MONTH);

int d1 = s.get(Calendar.DAY_OF_MONTH);

s.clear();

s.setTimeZone(z);

s.set(y1, m1, d1);

long now = s.getTime().getTime();

s.setTime(toSubtract);

int y2 = s.get(Calendar.YEAR);

int m2 = s.get(Calendar.MONTH);

int d2 = s.get(Calendar.DAY_OF_MONTH);

s.clear();

s.setTimeZone(z);

s.set(y2, m2, d2);

long t = s.getTime().getTime();

int result = (int)((now - t) / ONE_DAY);

TRACER.traceReturn("TimeAccess.subtractDate()", result);

return result;

}

I invoke the method a lot of times and check whether the returned value is greater than zero.

The algorithm works perfect excludes the following mysterious case:

if startDate is YYYY-MM-DD and toSubtract is YYYY-(MM+2)-(DD-2), the algorithm returns a positive value, which is false in my opinion.

Is there any conversion problem I did't thought about?

The platform is java 1.4.2_07-b05 on Sun Solaris

Thanks for your help in advance!

Best regards,

Balazs

[1786 byte] By [bAzsiToa] at [2007-10-1 17:15:42]
# 1
There are a few quirks with Calendar. Try calling Calendar.set(year, month, day) all in one go, rather than separately. See if that helps. - Saish
Saisha at 2007-7-11 1:58:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
I also got infos about Calendar problems. Is it possible that Calendar has static tags within it? If it is true, a multi-threaded application will go mad using Calendar.How can I call all in one? Only the get methods invoked separatelly, the set method is called in one step.
bAzsiToa at 2007-7-11 1:58:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

HI,

I tried to replicate your issue but it worked fine. How do you create your Date objects? This method turns Strings into Date's. You acn cahnge the format from yyyyMMdd to the input format you need.

import java.text.ParsePosition;

import java.text.SimpleDateFormat;

import java.util.Date;

public Date getDate(String stringDate) {

if (null == stringDate) {

return null;

}

// Expects format 20050628

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

return sdf.parse(stringDate, new ParsePosition(0));

}

leonarsta at 2007-7-11 1:58:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

Hi Leonarst,

unfortunatelly I can't tell how the date objects are constructed, it depend so deep in a database layer.

What I know is that these date objects are represents times in different time zones, and it must be converted to the same time zone to compare them well.

Also a strange thing that this error cannot reproduced by me, because it occurs only once in 10000... Very sporadic and mysterious :(

Is it possible that GregorianCalendar cannot be used well in multi-threaded environment?

bAzsiToa at 2007-7-11 1:58:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5
Where is the fucntion getStartDate() defined? It sounds like it might be a possible candidate for causing the problem.
stevel56a at 2007-7-11 1:58:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6
Hi stevel56getStartDate() is only returning the application start time (as a date object). It returns an object created during the start of application.Is it possible that this single instance swirled up something?
bAzsiToa at 2007-7-11 1:58:51 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...