Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to calculate the first and last day of each week

Tags:

java

calendar

I'm trying to create a weekly calendar that looks like this: http://dhtmlx.com/docs/products/dhtmlxScheduler/sample_basic.html

How can I calculate every week date? For example, this week is:

Monday - Sunday
7 June, 8 June, 9 June, 10 June, 11 June, 12 June, 13 June

like image 871
user236501 Avatar asked Jun 11 '10 13:06

user236501


1 Answers

I guess this does what you want:

// Get calendar set to current date and time
Calendar c = Calendar.getInstance();

// Set the calendar to monday of the current week
c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);

// Print dates of the current week starting on Monday
DateFormat df = new SimpleDateFormat("EEE dd/MM/yyyy");
for (int i = 0; i < 7; i++) {
    System.out.println(df.format(c.getTime()));
    c.add(Calendar.DATE, 1);
}
like image 111
Jesper Avatar answered Sep 28 '22 15:09

Jesper