Tuesday, July 28, 2009

Java : Get all Dates/Days between range of start date and end date

This is Simple java program to get all dates with days between range of start date and end date. Here date format used dd/MM/yyyy however any time you can customize it to your desire format. In below program just provide input start date and end date and see output.


/* ParseDate.java */

import java.util.*;
import java.text.*;

public class ParseDate
{
public static void main(String[] args)
{
String strdate = "25/07/2009";//any start date

String enddate = "03/08/2009";//any end date

SimpleDateFormat formatterDate = new SimpleDateFormat("dd/MM/yyyy");

SimpleDateFormat formatterDay = new SimpleDateFormat("dd/MM/yyyy EEEE");

Date st = null;
Date ed = null;

try
{
st = formatterDate.parse(strdate);
ed = formatterDate.parse(enddate);
}
catch (ParseException e)
{
System.out.println("Parse Exception :"+e);
}

Calendar ss=Calendar.getInstance();
Calendar ee=Calendar.getInstance();


ss.setTime(st);
ee.setTime(ed);
ee.add(Calendar.DATE,1);//just incrementing end date by 1

String day = "";

while(!ss.equals(ee))
{
day = formatterDay.format(ss.getTime());

System.out.println(day);

ss.add(Calendar.DATE,1);
}

}
}

/********INPUT************/

String strdate = "25/07/2009";//any start date

String enddate = "03/08/2009";//any end date

/********OUTPUT**********/

25/07/2009 Saturday
26/07/2009 Sunday
27/07/2009 Monday
28/07/2009 Tuesday
29/07/2009 Wednesday
30/07/2009 Thursday
31/07/2009 Friday
01/08/2009 Saturday
02/08/2009 Sunday
03/08/2009 Monday

No comments:

Post a Comment