Original Address:Java gets all the dates in a certain period of time
Author:Feier
Import Java.text.SimpleDateFormat;
Import java.util.ArrayList;
Import Java.util.Calendar;
Import Java.util.Date;
Import java.util.List;
public class Finddates
{
public static void Main (string[] args) throws Exception
{
Calendar cal = Calendar.getinstance ();
String start = "2012-02-01";
String end = "2012-03-02";
SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd");
Date Dbegin = Sdf.parse (start);
Date dend = Sdf.parse (end);
list<date> ldate = finddates (Dbegin, dend);
for (Date date:ldate)
{
System.out.println (Sdf.format (date));
}
}
public static list<date> finddates (date dbegin, date dend)
{
list ldate = new ARR Aylist ();
ldate.add (Dbegin);
calendar calbegin = Calendar.getinstance ();
//Sets the time for this Calendar with the given date
calbegin.settime (dbegin);
calendar Calend = Calendar.getinstance ();
//Sets the time for this Calendar using the given date
calend.settime (dend);
//test whether this date is after the specified date
while (Dend.after (Calbegin.gettime ()))
{
//According to the rules of the calendar, Adds or subtracts the specified amount of time for the given calendar field
calbegin.add (Calendar.day_of_month, 1);
ldate.add (Calbegin.gettime ());
}
return ldate;
}
}