wood burning stoves*
The moose likes Java in General and the fly likes dates between two dates Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "dates between two dates" Watch "dates between two dates" New topic
Author

dates between two dates

Nisha Kala
Greenhorn

Joined: Nov 17, 2010
Posts: 21
Hi all,
Is it possible in java to get all dates between two dates
for example
date1-> '01-11-2010'
date2-> '05-11-2010'
date3-> '10-11-2010'
Result :
'02-11-2010','03-11-2010','04-11-2010','06-11-2010','07-11-2010','08-11-2010','09-11-2010'
Can any one help me please..



Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You can try exploring the Calendar class. It may not provide a direct way, but there are lot of methods which can help you achieve this.


Mohamed Sanaulla | My Blog
Nisha Kala
Greenhorn

Joined: Nov 17, 2010
Posts: 21
thanks for your reply can you give me some brief idea about it
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You can take the input- Start date and end date. So from Start date onwards- Keep adding 1 day to the date till the date is less than your end date. You can use- the add() method in the Calendar class.
Nisha Kala
Greenhorn

Joined: Nov 17, 2010
Posts: 21
thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38509
    
  23
You can put all the Date objects into a List, and create an array from that List, if you wish.
Nisha Kala
Greenhorn

Joined: Nov 17, 2010
Posts: 21
thanks for your reply
I tried using list and am getting the missing dates..
But am not able to find the missing months..
example:
date1-> '11-11-2010'
date2->'02-01-2011'

I want to get all the dates between these two date
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Nisha Kala wrote: ... But am not able to find the missing months.. ...

I don't know what you mean by Missing months. But using add() wil automatically increment the larger fields- Like month, year.
Nisha Kala
Greenhorn

Joined: Nov 17, 2010
Posts: 21
I tried add function.. am not able to it..
I got dates as a list of String..
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Nisha Kala wrote:I tried add function.. am not able to it..
I got dates as a list of String..


You have to be careful with the format of the dates. It will be easier if you have your startDate and endDate in java.util.Date and not in String. Otherwise you can use SimpleDateFormat and a correct pattern to convert them to Date instances. Once you have the Start and End dates in Date format. The next step would be:


Note that Calendar by default uses the following format: Mon Nov 22 12:24:44 IST 2010. You can check out how to use SimpleDateFormat here: http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Its always easier to convert from Date to String.
Nisha Kala
Greenhorn

Joined: Nov 17, 2010
Posts: 21
thank you..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dates between two dates