• 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

Evaluating and comparing dates

 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've never really had to deal with dates before so please bear with me if this is a silly question.
I'm writing an application where the user will enter their date and time of birth.
I need to then evaluate which of a set range of dates their birthdate falls into.
i.e. a range may be "23 June 1987" - "1 July 1987" so "30 June 1987" would fall into this range.
I've had a look at some of the Calender and Date classes and i think that what really complicates the situation is this ....
The total amount of information I need is BirthDate and BirthTime.
However depending on where you are in my application different parts of this information become irrelevent.
e.g. Sometimes the year and time doesn't matter and i just want to see if the date falls within a range such as June 30 - July 15
other times the date is important but the time isn't.
and other times the whole set of information is required.
I've no idea how to implement this. Does anyone else?
 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you need to look at what you really need and when you need it first. Find out when you need both the time and date and when you just need the date. If you only need the date then try using the java.util.Date class. The after and before methods should help you out
http://localhost:49213/cgi-bin/vahwebx.exe/vahelp/vj32/Extract/0/j2sdk /ref/java.util.Date_dsc.html#_top_
--------------------------------------
after
public boolean after(Date when)
Tests if this date is after the specified date.
Parameters:
when - a date.
Returns:
true if and only if the instant represented by this Date object is strictly later than the instant represented by when; false otherwise.
before
public boolean before(Date when)
Tests if this date is before the specified date.
Parameters:
when - a date.
Returns:
true if and only if the instant of time represented by this Date object is strictly earlier than the instant represented by when; false otherwise.
[This message has been edited by Dale DeMott (edited November 01, 2001).]
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dale, that's great, but what about when I don't want the year considered?
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm assuming that in certain spots in your app you exclusively will not want to consider the year. In these parts of your app, before you do your compare, artificially set the year to be the same in both dates. Example, set both firstDate and secondDate to have a year of 1900. Then you can compare to see if the date falls between x date and y date. (assuming you are ignoring the year). Does this help?
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
[This message has been edited by Dale DeMott (edited November 01, 2001).]
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm .. nice idea!! Thank you.
Even managed to suggest methods that aren't deprecated yet.
Might struggle thought if my range was 15 Dec - 15 Jan for instance, but I'm sure I can figure something out.
I may just have to write my own date utility class!!
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can see that being an issue. That all depends on your business logic and how you are using it.
Regards,
Dale DeMott
------------------
By failing to prepare, you are preparing to fail.
Benjamin Franklin (1706 - 1790)
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela, rather than rolling a Date-substitute class, you could create a Comparator object for each type of comparison you want to do. Not only does that allow you to keep the messy bits separate, it also gives you the opportunity to put your datetimes into sorted collections as/if/when the need arises, and it allows you to use the comparisons with any Date.
Don't fall into the trap of using java.sql.Time and java.sql.Date. Both are still java.util.Date's underneath and both do have both time and date parts (even though they are not printed by toString()). No matter what the javadoc suggests.
Implementing the comparators is going to be a pain. I can't come up with a better suggestion than that already given (I think) - instantiate a Calendar, set the time, clear the fields you don't want to compare, call getTime() to get the corresponding Date and compare the dates so constructed.
If you have more than three different Comparators that need to be created, try to generalise it: write a Comparator that takes an array of fields that need to be compared. So you'd use "new DateFieldComparator(new int[] {Calendar.YEAR, Calendar.MONTH})" to create a Comparator that only compares on year and month, disregarding day and time. If you can't figure it out, drop me a note or post.
And once you've written it, share the code and become famous and popular overnight
- Peter

[This message has been edited by Peter den Haan (edited November 01, 2001).]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I've never really had to deal with dates
> before so please bear with me if this is a
> silly question
Limited dating experience. I understand.
Peter's suggestions regarding comparators are right on target.
One problem with setting the year to 1900 is that there's no way to represent Feb 29 in this system. If you go this route, I suggest you use 2000 (or any other leap year) instead.
Regarding the question of date wrap - you can just invert the order of the dates and then negate the result. That is, another way of asking "is this date between Dec 15 and Jan 15?" is "is this date not between Jan 15 and Dec 15?" The second form can be expressed with all the dates in the same year, while the first cannot.
[This message has been edited by Jim Yingst (edited November 01, 2001).]
 
Popeye has his spinach. I have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic