Solved

Access dates between two dates

Posted on 2007-12-03
6
1,229 Views
Last Modified: 2008-03-11
Hi,

I have two dates:

Date From - 01/01/2008
Date To  - 4/01/2008

How can I output all of the dates between the two. So my output would be :
01/01/2008
02/01/2008
03/01/2008
04/01/2008

Thanks
0
Comment
Question by:monitorscreen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 23

Expert Comment

by:cmalakar
ID: 20395921
Use Calendar's add() api...

Calendar obj = Calendar.getInstance();
obj.set(2001, 01, 01);
obj.add(Calendar.DATE, 1);

print the date...
you can keep on adding till you get the required date...

You can come to know the current day, month, year by using get() method of Calendar Class.
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20395955
you can use the DateFormat and SimpleDateFormat for printing the date in your required format...
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20396048
probably you can do this below,

Date date1 = DateFormat.getInstance().parse( "01/01/2008" ) ;
Date date2 = DateFormat.getInstance().parse( "04/01/2008" ) ;

Calendar cal1 = Calendar.getInstance();
cal1.setTime( date1 );
Calendar cal2 = Calendar.getInstance();
cal2.setTime( date2 );

while( ! isSameDay( cal1, cal2 ) ){
  //print the cal1 date here
  cal1.add( Calendar.DATE, 1 ) ;
}

public static boolean isSameDay( Calendar cal1, Calendar cal2 ){
   return cal1.get( 0 ) == cal2.get( 0 ) && cal1.get( 1 ) == cal2.get( 1 ) && cal1.get( 6 ) == cal2.get( 6 );
      }
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 20397338
You only need one Calendar:


DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = df.parse("01/01/2008") ;
Date date2 = df.parse("04/01/2008") ;
 
Calendar cal = Calendar.getInstance();
cal.setTime( date1 );
while(!cal.getTime().after(date2)) {
  //print the cal1 date here
  cal.add( Calendar.DATE, 1 ) ;
}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 20399035
expanding on what cmalakar and others have posted, to get the output you need use something like this:

DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = df.parse("01/01/2008") ;
Date date2 = df.parse("04/01/2008") ;
 
Calendar cal = Calendar.getInstance();
cal.setTime( date1 );
while(cal.getTime().before(date2) || cal.getTime().equals(date2)) {
  System.out.println(df.format(cal.getTime());
  cal.add( Calendar.DATE, 1 ) ;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21094626
:-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question