jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes is that any utility class that help compare date within start and end date ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "is that any utility class that help compare date within start and end date ?" Watch "is that any utility class that help compare date within start and end date ?" New topic
Author

is that any utility class that help compare date within start and end date ?

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 439
hi, good day, i'm actually want to compare a date see whether is the date fall into the range between start date and end date , is that any utility class in apache common that can help up this ? or anyone can suggest the idea to compare ? thank you for guidance
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12789
    
    5
What format is the date in?
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Are java.util.Date objects java.lang.Comparable


There is no emoticon for what I am feeling!
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 439
William Brogden , the format is dd/MM/yyyy

scenario :

i need to know if given a new date range, and check whether this new date range is fall into between the date range

for example ,

new start_date = 15/05/2006, new end_date = 01/09/2006

existing start_date = 20/02/2006
existing end_date = 11/07/2006

so in this case , new start and end date have fall into existing start and end date range, cause it should not interlapse the date range
- in short, new start_date and end_date cannot in between of existing start_date and end_date , how i write a algorithm on this ? thank you for guidance
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Take a look at SimpleDateFormat. It can parse a string into a Date, then as I mentioned, dates are comparable.
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 439
jeff, i facing more on the problem of algorithm that check the date range

below is my try code :


this is simple testing on main function
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 439
thanks jeff for great job , it look so easy for the solution , damn, i'm too stupid as keep thinking of taking care on both start_date and end_date

so what i need is just compare for each start_date and end_date , if either one provide false result , means it is in date range

my curious , what if i want to make method by passing compareStartDt and compareEndDt as follow :



testing :


it give me isRange = false , how to tackle this ?

thank you again for your guidance
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Are you checking two ranges for overlap? See if this makes sense:

What would you think about making a DateRange class with methods like:


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 439
thanks , Stan James for idea , just found that Jeff Albertson 's sample have not fix when it is in same date

i try modify as follow :


and with testing


it couldn't work properly ...anyone have idea ? thanks again for all of you about contribution
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Since your comparison method is mainly for comparing, you shouldn't keep the conversion codes in it.

There're only 4 situations whereby 2 date ranges overlap:
1. Range 1 is larger than Range 2 entirely
2. Range 1 is the same as Range 2
3. Range 1's end date is the same or later than Range 2's start date
4. Range 1's start date is the same or earlier than Range 2's end date

Focus on getting these conditions right first, without bothering about the conversion from String to Date objects. Write a separate method to do the conversion. This way, you would know which is the method that is wrong.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Testing for your conditions #3 && #4 -> true is sufficient. My example was the negative of the same per DeMorgan: !#3 || !#4 -> false.

If I were the mentor or teacher in this situation, I'd love to see:

Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Originally posted by Stan James:
Testing for your conditions #3 && #4 -> true is sufficient.


Agreed, testing for #3 & #4 should suffice in this situation. Get your algorithm up & right and the codes would come naturally.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: is that any utility class that help compare date within start and end date ?