Build a list containing all the days of the week for any month in any year. Saturdays and Sundays should not be on this list. Write a class containing the methods necessary to build such a list.

How now to remove Sat and Sun?

import java.text.DateFormatSymbols; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; public class Task5 extends AbstractTableModel{ int column; int row; Integer days[][]; Calendar calendar; String[] dayNames; int year; int month; public Task5(int year, int month){ set(year, month); } public void set(int year, int month){ this.year = year; this.month = month; init(); fireTableStructureChanged(); } private void init(){ calendar = Calendar.getInstance(); calendar.setMinimalDaysInFirstWeek(7); calendar.clear(); calendar.set(Calendar.YEAR, year); calendar.set(Calendar.MONTH, month); column = calendar.getActualMaximum(Calendar.DAY_OF_WEEK) - calendar.getActualMinimum(Calendar.DAY_OF_WEEK) + 1; row = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH) - calendar.getActualMinimum(Calendar.WEEK_OF_MONTH) + 1; days = new Integer[row][column]; dayNames = new String[column]; int currDay = calendar.getFirstDayOfWeek(); String[] d = new DateFormatSymbols().getShortWeekdays(); for(int i = 0; i < dayNames.length; i++){ dayNames[i] = d[currDay]; currDay++; if(currDay > calendar.getActualMaximum(Calendar.DAY_OF_WEEK)) currDay = calendar.getActualMinimum(Calendar.DAY_OF_WEEK); } calendar.set(Calendar.WEEK_OF_MONTH, calendar.getActualMinimum(Calendar.WEEK_OF_MONTH)); calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); for(int r = 0; r < row; r++){ for(int c = 0; c < column; c++){ if(calendar.get(Calendar.MONTH) == month){ days[r][c] = calendar.get(Calendar.DAY_OF_MONTH); } else days[r][c] = null; calendar.add(Calendar.DAY_OF_YEAR, 1); } } } public Object getValueAt(int rowIndex, int columnIndex){ return days[rowIndex][columnIndex]; } public Class getColumnClass(int columnIndex){ return Integer.class; } public int getRowCount(){ return row; } public int getColumnCount(){ return column; } public String getColumnName(int columnIndex){ return dayNames[columnIndex]; } public static void main(String[] args){ Task5 model = new Task5(2013, Calendar.AUGUST); JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new JScrollPane(new JTable(model))); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } } 

    1 answer 1

    You just need to know which day of the week is the first day of the month, and then in a cycle from 1 to the last day of the month, add five days to the first day.
    For each day in the cycle it makes no sense to find out the day of the week.