Click to See Complete Forum and Search --> : [RESOLVED] Calendar week view


Ultimatum
January 7th, 2010, 03:10 PM
I want to create a week view but I can't figure out how to get the first date of this week (03/01/2010) and the last date (09/01/2010). I found this script but I get as first date 10/01/2010 and as last date 09/01/2010 and I don't see why I get 10/01/2010 as first date.

// Code to test it.
GregorianCalendar calendar = new GregorianCalendar();

//Get the interval
String [] interval = calendarModel.getWeekInterval(calendar);

System.out.println("First day of week: " + interval[0]);
System.out.println("Last day of week: " + interval[1]);

private DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
public String[] getWeekInterval(Calendar cal)
{
// find start of week

Calendar start = Calendar.getInstance();
start.setTime(cal.getTime());
start.set(Calendar.DAY_OF_WEEK, cal.getActualMinimum(Calendar.DAY_OF_WEEK));

// find end of week

Calendar end = Calendar.getInstance();
end.setTime(cal.getTime());
end.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));

// format start and end date and return

return new String[] { df.format(start.getTime()), df.format(end.getTime()) };
}

private Calendar getFirstDayOfThisWeek(GregorianCalendar cal)
{
cal.add(Calendar.DAY_OF_YEAR, Calendar.SATURDAY);

return cal;
}

private Calendar getLastDayOfThisWeek(GregorianCalendar cal)
{
cal.add(Calendar.DAY_OF_YEAR, Calendar.SATURDAY);

return cal;
}

Deliverance
January 8th, 2010, 09:32 AM
Pay attention to what you are reading and displaying. Using today's date arbitrarily you get:

01/03/2010 and 01/09/2010

as the boundaries.

I should mention now that


private Calendar getFirstDayOfThisWeek(GregorianCalendar cal)
{
cal.add(Calendar.DAY_OF_YEAR, Calendar.SATURDAY);

return cal;
}

private Calendar getLastDayOfThisWeek(GregorianCalendar cal)
{
cal.add(Calendar.DAY_OF_YEAR, Calendar.SATURDAY);

return cal;
}


are both the same method, they do the same thing, and you should realize that you are adding to the current date object a year modifier, with the constant Calendar.SATURDAY which is equal to 7.

Deliverance
January 8th, 2010, 12:44 PM
Didn't think to post this the first time around, but if you want a full understanding of date and time as it relates to Java and the various wrapper classes it provides, this is a very informative and a good read for you, or anyone for that matter to go over:

http://www.odi.ch/prog/design/datetime.php

Ultimatum
January 14th, 2010, 11:16 PM
Thank you, that link helped me a lot. I fixed it now