Win a copy of Design for the Mind this week in the Design forum!
    Bookmark Topic Watch Topic
  • New Topic

How to check a date is within range of dates ?

 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi All,

I have a date for example 2007/10/04 in YYYY/MM/DD format,
and start date as 2008/08/12(YYYY/MM/DD),
end date as 2008/08/19(YYYY/MM/DD).

How to check wheather this date 2007/10/04 falls between 2008/08/12 and 2008/08/19?

Thanks & Regards,
Deepak
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Report post to moderator
One way to do this would be:

1. Parse all those strings with dates into Date objects using SimpleDateFormat
2. Use the before() and/or after() methods of class Date to compare the Date objects
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Report post to moderator
I presume these date's are represented as Strings. If so, parse them to java.util.Date [1] objects using the (inherited) parse(String) method from the java.text.SimpleDateFormat [2] class.
Now you have three Date objects and you can see if one of them lies in between the other two dates by using Date's before(Date) and after(Date) methods.

Good luck.

[1] http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html
[2] http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html
 
Deepak Kumar
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Report post to moderator
Thanks a lot, Its working!!!
 
nin hat
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Report post to moderator
...
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Report post to moderator
nin hat wrote:

Please check your private messages for an important administrative matter

I am sorry, but I have edited out your post. Please do not hijack other people's threads to ask your questions.
You can post your problem by starting a new thread.
UseOneThreadPerQuestion

Also, please do take out time to read http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
    Bookmark Topic Watch Topic
  • New Topic