• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date comparision problem

 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know, but have a look at this very recent thread, and see whether it helps at all.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50175
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and try the compareTo method; I think the date-type classes implement the Comparable<T> interface.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getStartDate() is the selected date by the user from the calender.
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare your constants MILLISECONDS_IN_DAY and DAYS_THREE_MONTHS to be long instead of int.
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is declared as long only.

Thanks
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot !!!

Solution worked for me.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic