aspose file tools*
The moose likes Java in General and the fly likes Date comparision problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date comparision problem" Watch "Date comparision problem" New topic
Author

Date comparision problem

Maria Laxmi
Ranch Hand

Joined: Aug 08, 2008
Posts: 40
Hi,

I am trying to compare two dates in my program, where the current date is being compared with the selected date by the user from the calender. The selected date should not be more than 90 days from the current date. The function which i have defined gives correct result if the condition contains less than 68 days comparison, means instead of 90 days if you put 68 or less than 68 then the function gives the correct result but Once you give more than 68 days comparison the result I am getting is wrong. I don't understand where is the problem. Does any one have any idea what is wrong with this?

Here is the code I am using.

public boolean test1(){
long date = getCurrentDate();
return getStartDate() != null && getStartDate().getTime() > date + MILLISECONDS_IN_DAY * DAYS_THREE_MONTHS;
}

getCurrentDate(){
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal.getTimeInMillis();
}

Thanks,
[ August 27, 2008: Message edited by: Bear Bibeault ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Don't know, but have a look at this very recent thread, and see whether it helps at all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
. . . and try the compareTo method; I think the date-type classes implement the Comparable<T> interface.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3739
    
  16
What does getStartDate() do ? You call it twice in your return statement, so if the first call does anything that could affect the second call, it may not be returning what you expected.


Joanne
Maria Laxmi
Ranch Hand

Joined: Aug 08, 2008
Posts: 40
getStartDate() is the selected date by the user from the calender.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Declare your constants MILLISECONDS_IN_DAY and DAYS_THREE_MONTHS to be long instead of int.
Maria Laxmi
Ranch Hand

Joined: Aug 08, 2008
Posts: 40
It is declared as long only.

Thanks
Maria Laxmi
Ranch Hand

Joined: Aug 08, 2008
Posts: 40
Hi,

I think I got the reason for this problem. But I am not sure how could it be resolved?

Problem is with the Daylight Time change in the United States. The logic explained above creates problem when adding 90 days to the current days gives any of the day after the 1st Sunday of November when clocks are set back to one hr.

Does any one have any idea how to deal with this?

Thanks,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Yeah. Don't add DAYS * MILLISPERDAY because different days have different numbers of milliseconds. In particular the day when DST starts has only 23 hours worth of milliseconds and the day when DST ends has 25 hours worth of milliseconds.

Create a calendar object and add a number of days to it instead.
Maria Laxmi
Ranch Hand

Joined: Aug 08, 2008
Posts: 40
Thanks a lot !!!

Solution worked for me.
 
 
subject: Date comparision problem