P
pit.grinja
Hi there,
For a custom dialog that will allow users to select a date I wrote a
JTable that displays the days of one complete month. The JTable gets an
instance of GregorianCalendar as parameter. The table has seven
columns, first header is "Sun",last header is "Sat". To correctly
determine the number of rows, I wanted to use the method
getActualMaximum(Calendar.WEEK_OF_MONTH). However, for some months, the
return value is less than I would expect. Here is a sample program:
import java.util.*;
public class DatePrinter{
private GregorianCalendar cal;
private final String[] WEEKDAYS =
{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
public DatePrinter(){
this.cal = new GregorianCalendar();
cal.setFirstDayOfWeek(Calendar.SUNDAY);
int year = this.cal.get(Calendar.YEAR);
int month = this.cal.get(Calendar.MONTH);
int firstWeekdayOfMonth = (new
GregorianCalendar(year,month,1).get(Calendar.DAY_OF_WEEK));
System.out.println("First Day of Month:
"+WEEKDAYS[firstWeekdayOfMonth-1]);
System.out.println("Day: "+this.cal.get(Calendar.DAY_OF_MONTH));
System.out.println("Month: "+(this.cal.get(Calendar.MONTH)+1));
System.out.println("Year: "+this.cal.get(Calendar.YEAR));
System.out.println("Weeks in actual month:
"+this.cal.getActualMaximum(Calendar.WEEK_OF_MONTH));
System.out.println("Number of days in actual month:
"+this.cal.getActualMaximum(Calendar.DAY_OF_MONTH));
}
public static void main(String[] ARGS){
DatePrinter dp = new DatePrinter();
}
}
//end of code
For october, I would expect a value of 6 for
this.cal.getActualMaximum(Calendar.WEEK_OF_MONTH): first week: just the
saturday of october 1st, 2nd week: oct 2-8, 3rd week: 9-15,... 6th
week: 30-31. But the return value is 5. For january, april,
july,september and december, i also get wrong (i.e. to small) values.
Is this a flaw in Java? I am using 1.4.2_08b03 on Win2000. Or is my
thinking wrong?
Many thanks for any pointers
Piet
For a custom dialog that will allow users to select a date I wrote a
JTable that displays the days of one complete month. The JTable gets an
instance of GregorianCalendar as parameter. The table has seven
columns, first header is "Sun",last header is "Sat". To correctly
determine the number of rows, I wanted to use the method
getActualMaximum(Calendar.WEEK_OF_MONTH). However, for some months, the
return value is less than I would expect. Here is a sample program:
import java.util.*;
public class DatePrinter{
private GregorianCalendar cal;
private final String[] WEEKDAYS =
{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
public DatePrinter(){
this.cal = new GregorianCalendar();
cal.setFirstDayOfWeek(Calendar.SUNDAY);
int year = this.cal.get(Calendar.YEAR);
int month = this.cal.get(Calendar.MONTH);
int firstWeekdayOfMonth = (new
GregorianCalendar(year,month,1).get(Calendar.DAY_OF_WEEK));
System.out.println("First Day of Month:
"+WEEKDAYS[firstWeekdayOfMonth-1]);
System.out.println("Day: "+this.cal.get(Calendar.DAY_OF_MONTH));
System.out.println("Month: "+(this.cal.get(Calendar.MONTH)+1));
System.out.println("Year: "+this.cal.get(Calendar.YEAR));
System.out.println("Weeks in actual month:
"+this.cal.getActualMaximum(Calendar.WEEK_OF_MONTH));
System.out.println("Number of days in actual month:
"+this.cal.getActualMaximum(Calendar.DAY_OF_MONTH));
}
public static void main(String[] ARGS){
DatePrinter dp = new DatePrinter();
}
}
//end of code
For october, I would expect a value of 6 for
this.cal.getActualMaximum(Calendar.WEEK_OF_MONTH): first week: just the
saturday of october 1st, 2nd week: oct 2-8, 3rd week: 9-15,... 6th
week: 30-31. But the return value is 5. For january, april,
july,september and december, i also get wrong (i.e. to small) values.
Is this a flaw in Java? I am using 1.4.2_08b03 on Win2000. Or is my
thinking wrong?
Many thanks for any pointers
Piet