challenge 年間カレンダー

nを入力としてn年の年間カレンダーを返すプログラムを作ってください
少なくとも日曜日と土曜日が判別出来るようにしてください
出力は標準出力でもファイルでも構いません
デザインは各自のお好みで

出力例1:
(y-calendar 2008)=>
#=Saturday, @=Sunday
2008/1 1 2 3 4 #5 @6 7 ...
2008/2 1 #2 @3 4 5 6 7 ...
...
2008/12 1 2 3 4 5 #6 @7 ...

出力例2:
(y-calendar 2008)=>
        M T W T F S S M
2008/ 1   1 2 3 4 5 6 7 ...
2008/ 2         1 2 3 4 ...
...
2008/12 1 2 3 4 5 6 7 8 ...

出力例3:
(y-calendar 2008)は2008.htmlを出力する
2008.htmlの中身
----
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>2008 calendar</title>
<style type="text/css">
* {font-family: monospace;}
span {margin: 0px 3px;}
span.sunday {color:red;font-weight:bold;}
span.saturday {color:blue;font-weight:bold;}
dd ul li{display:inline;}
</style>
</head>
<body>
<h1>2008 calendar</h1>
<dl>
<dt>2008/1</dt>
<dd><ul>
<li><span class="weekday">1</span></li>
<li><span class="weekday">2</span></li>
<li><span class="weekday">3</span></li>
<li><span class="weekday">4</span></li>
<li><span class="saturday">5</span></li>
<li><span class="sunday">6</span></li>
...
</ul></dd>
...
</dl>
</body>
</html>
----

Posted feedbacks - Java

Java/Swingで組んでみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;

class MeApp {
    public static void main(String[] args) {
        final int year;
        if (args.length == 0) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        } else {
            year = Integer.valueOf(args[0]);
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final MeFrame frame = new MeFrame(year);
                frame.setDefaultCloseOperation(MeFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class MeFrame extends JFrame {
    public MeFrame(int year) {
        super(String.format("%d年 年間カレンダー", year));
        getContentPane().setLayout(new GridLayout(3, 4, 5, 5));
        for (int i = 0; i < 12; ++i) {
            getContentPane().add(new MeMonthPanel(year, i + 1));
        }
    }
}

class MeMonthPanel extends JPanel {
    public MeMonthPanel(int year, int month) {
        setLayout(new BorderLayout());
        add(new JLabel(String.format("%d月", month), JLabel.CENTER), BorderLayout.NORTH);
        add(new MeMonthCanvas(year, month), BorderLayout.CENTER);
    }
}

class MeMonthCanvas extends JComponent {
    private final String[][] _cells = new String[7][7];
    private final Color[][] _colors = new Color[7][7];
    public MeMonthCanvas(int year, int month) {
        final String[] weekdays = new DateFormatSymbols().getShortWeekdays();
        for (int col = 0; col < 7; ++col) {
            _cells[0][col] = weekdays[col + 1];
            _colors[0][col] = new Color(231, 255, 231);
        }
        _colors[0][0] = new Color(247, 202, 202); // Sunday
        _colors[0][6] = new Color(206, 255, 255); // Saturday

        final Calendar cal = Calendar.getInstance();
        for (int row = 1; row < 7; ++row) {
            for (int col = 0; col < 7; ++col) {
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, month - 1);     // starts from 0
                cal.set(Calendar.DAY_OF_WEEK, col + 1); // starts from 1
                cal.set(Calendar.WEEK_OF_MONTH, row);   // starts from 1
                if (cal.get(Calendar.DAY_OF_WEEK) == col + 1 && cal.get(Calendar.WEEK_OF_MONTH) == row) {
                    _cells[row][col] = String.valueOf(cal.get(Calendar.DATE));
                } else {
                    _cells[row][col] = "";
                }
                _colors[row][col] = Color.WHITE;
            }
        }
    }
    public Dimension getPreferredSize() {
        return new Dimension(25 * 7, 25 * 7);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        final FontMetrics fm = g.getFontMetrics();
        final int w = getWidth() / 7;
        final int h = getHeight() / 7;
        for (int row = 0; row < 7; ++row) {
            for (int col = 0; col < 7; ++col) {
                g.setColor(_colors[row][col]);
                g.fill3DRect(w * col, h * row, w, h, true);
                final String s = _cells[row][col];
                g.setColor(Color.BLACK);
                g.drawString(
                    s,
                    w * col + (w - fm.stringWidth(s)) / 2,
                    h * row + (h - fm.getHeight()) / 2 + fm.getAscent()
                );
            }
        }
    }
}

ついでにJTable版。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.awt.*;
import java.text.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

class MeApp {
    public static void main(String[] args) {
        final int year;
        if (args.length == 0) {
            year = Calendar.getInstance().get(Calendar.YEAR);
        } else {
            year = Integer.valueOf(args[0]);
        }
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                final MeFrame frame = new MeFrame(year);
                frame.setDefaultCloseOperation(MeFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
            }
        });
    }
}

class MeFrame extends JFrame {
    public MeFrame(int year) {
        super(String.valueOf(year));
        getContentPane().setLayout(new GridLayout(3, 4, 5, 5));
        for (int i = 0; i < 12; ++i) {
            getContentPane().add(new MeMonthPanel(year, i + 1));
        }
    }
}

class MeMonthPanel extends JPanel {
    public MeMonthPanel(int year, int month) {
        setLayout(new BorderLayout());
        add(new JLabel(new DateFormatSymbols().getMonths()[month - 1], JLabel.CENTER), BorderLayout.NORTH);
        add(new JScrollPane(new JTable(new MeMonthTableModel(year, month))), BorderLayout.CENTER);
    }
    public Dimension getPreferredSize() {
        return new Dimension(220, 180); // ???
    }
}

class MeMonthTableModel extends AbstractTableModel {
    private final int _year, _month;
    private final String[] _weekdays = new DateFormatSymbols().getShortWeekdays();
    private final Calendar _cal = Calendar.getInstance();

    public MeMonthTableModel(int year, int month) {
        _year = year; _month = month;
    }
    public int getColumnCount() {
        return 7;
    }
    public String getColumnName(int col) {
        return _weekdays[col + 1];
    }
    public int getRowCount() {
        return 6;
    }
    public Object getValueAt(int row, int col) {
        _cal.set(Calendar.YEAR, _year);
        _cal.set(Calendar.MONTH, _month - 1);      // starts from 0
        _cal.set(Calendar.DAY_OF_WEEK, col + 1);   // starts from 1
        _cal.set(Calendar.WEEK_OF_MONTH, row + 1); // starts from 1
        if (_cal.get(Calendar.DAY_OF_WEEK) == col + 1 && _cal.get(Calendar.WEEK_OF_MONTH) == row + 1) {
            return String.valueOf(_cal.get(Calendar.DATE));
        } else {
            return "";
        }
    }
}

#5080を参考に、Javaの標準出力版を書いてみました。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.text.DateFormatSymbols;
import java.util.Calendar;
import java.util.Locale;

public class Sample119 {
    private final int year_;
    private final Locale locale_;
    private DateFormatSymbols formatSymbols_;

    public Sample119(int year, Locale locale) {
        year_ = year;
        locale_ = locale;
        formatSymbols_ = new DateFormatSymbols(locale_);
    }

    public void printCalendar() {
        System.out.println(locale_.getDisplayCountry());
        for (int month = 0; month < 12; month++) {
            printCalendar(month);
        }
        System.out.println();
    }
    private void printCalendar(int month) {
        System.out.format("< %s/%d >%n", formatSymbols_.getShortMonths()[month], year_);
        String[] weekdays = formatSymbols_.getShortWeekdays();
        for (int index = 1; index < weekdays.length; index++) {
            System.out.format("%s ", weekdays[index]);
        }
        System.out.println();

        Calendar cal = Calendar.getInstance(locale_);
        cal.set(year_, month, 1);
        int dayLen = weekdays[1].getBytes().length;
        for (int index = 0, length = (cal.get(Calendar.DAY_OF_WEEK) - 1) * (dayLen + 1); index < length; index++) {
            System.out.print(" ");
        }
        String format = String.format("%%%dd ", dayLen);
        for (; cal.get(Calendar.MONTH) == month; cal.add(Calendar.DATE, 1)) {
            System.out.format(format, cal.get(Calendar.DATE));
            if (Calendar.SATURDAY == cal.get(Calendar.DAY_OF_WEEK)) {
                System.out.println();
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {
        new Sample119(2008, Locale.JAPAN).printCalendar();
        new Sample119(2008, Locale.US).printCalendar();
    }
}

Index

Feed

Other

Link

Pathtraq

loading...