JSF Marquee Component | Java Example | FlexiGrid using Servlet + JSON |

First day of Week / Month for a given date range

leave a comment »

public class DateTest {

public static void main(String[] args) throws Exception {

String date1 = “01/21/2011”;
String date2 = “07/1/2011”;
SimpleDateFormat sdf = new SimpleDateFormat(“MM/dd/yyyy”);

List dateList = getFirstDaysDate(“monthly”, sdf.parse(date1), sdf.parse(date2));
System.out.println(” dateList ? ” + dateList);
}

public static List getFirstDaysDate(String type, Date from_date, Date to_date) {

List dateList = new ArrayList();
SimpleDateFormat sdf = new SimpleDateFormat(“MM-dd-yyyy”);

// Get calendar, clear it and set week number and year.
Calendar calendar = new GregorianCalendar();
calendar.setTime(from_date);

if (“weekly”.equals(type)) {
calendar.set(Calendar.DAY_OF_WEEK,
calendar.getActualMinimum(Calendar.DAY_OF_WEEK));
Date firstDayOfTheWeek = calendar.getTime();
dateList.add(sdf.format(firstDayOfTheWeek));
while (firstDayOfTheWeek.before(to_date)) {
calendar.add(Calendar.DATE , 7);
firstDayOfTheWeek = calendar.getTime();
if ( ! firstDayOfTheWeek.after(to_date)) {
dateList.add(sdf.format(firstDayOfTheWeek));
}
}
} else if (“monthly”.equals(type)) {
calendar.set(Calendar.DAY_OF_MONTH,
calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
Date firstDayOfTheMonth = calendar.getTime();
System.out.println(” first day of month ” + firstDayOfTheMonth);
dateList.add(sdf.format(firstDayOfTheMonth));
while (firstDayOfTheMonth.before(to_date)) {
calendar.add(Calendar.MONTH , 1);
firstDayOfTheMonth = calendar.getTime();
if ( ! firstDayOfTheMonth.after(to_date)) {
dateList.add(sdf.format(firstDayOfTheMonth));
}
}
}

return dateList;
}
}

Advertisements

Written by speedrace703

June 29, 2011 at 11:36 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: