Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access dates between two dates

Posted on 2007-12-03
6
Medium Priority
?
1,243 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

604 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