• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date comparision problem

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and try the compareTo method; I think the date-type classes implement the Comparable<T> interface.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
getStartDate() is the selected date by the user from the calender.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is declared as long only.

Thanks
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot !!!

Solution worked for me.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic