The moose likes Java in General and the fly likes How to check a date is within range of dates ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

How to check a date is within range of dates ?

Deepak Kumar
Ranch Hand

Joined: Nov 05, 2007
Posts: 62
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,

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

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

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
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

Joined: Nov 05, 2007
Posts: 62
Thanks a lot, Its working!!!
nin hat

Joined: Mar 15, 2010
Posts: 1
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10170

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.

Also, please do take out time to read http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
I agree. Here's the link: http://aspose.com/file-tools
subject: How to check a date is within range of dates ?
Similar Threads
Date function
Code review required for converting dateformat
date validation
Info on Sun Vouchers
date conversion problem