• 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

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

 
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What format is the date in?
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are java.util.Date objects java.lang.Comparable
 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at SimpleDateFormat. It can parse a string into a Date, then as I mentioned, dates are comparable.
 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you checking two ranges for overlap? See if this makes sense:

What would you think about making a DateRange class with methods like:
 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic