?
Solved

Access dates between two dates

Posted on 2007-12-03
6
Medium Priority
?
1,235 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Accepted Solution

by:
CEHJ earned 1000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

765 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