Solved

Access dates between two dates

Posted on 2007-12-03
6
1,191 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
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now