Time4j:显示一年中的所有日子

Time4j: Display all days of a year

我为 Java 使用库 Time4j,我选择了一些日子作为节日并保存在变量中。现在我想显示一年中的所有日子,类似于图片,但突出显示我的节日。

我怎样才能做到?

我提供了一个仅适用于一个月的 Time4J 示例,可以很容易地应用于整个日历年的每个月。 Time4J 没有明确支持假期,但我提供了一个简单的 HTML-示例如何突出显示特殊日子,这里是周末。

关于假期: 如果您定义了每年的假期列表,那么您可以使用 boolean isHoliday = holidayList.contains(date) 这样的表达式来询问日期是否为假期.顺便说一句,最新版本的Time4J (v3.16/v4.13) 也支持computation of Easter,所以每年的任何假期的计算在西方地区应该不会那么困难。

已更新以实现需要使用两周模型的本地化周开始:

public static void main(String[] args) {
    // locale for determining weekends and start of week
    Locale locale = Locale.US;

    // we start by Sunday in first column of calendar (US-model)
    // and need one day only for displaying any calendar row (table view)
    Weekmodel weekmodel = Weekmodel.of(locale);
    Weekmodel rowmodel = Weekmodel.of(weekmodel.getFirstDayOfWeek(), 1);

    // our input configuration
    int year = 2016;
    Month month = Month.JANUARY;

    // determine limits of month
    PlainDate start = PlainDate.of(year, month, 1); // first day of month
    PlainDate end = start.with(PlainDate.DAY_OF_MONTH.maximized()); // last day of month

    // we choose bounded-week because we need a monotonously increasing number
    int rowMin = start.get(rowmodel.boundedWeekOfMonth());
    int rowMax = end.get(rowmodel.boundedWeekOfMonth());

    // table creation
    Object[][] table = new Object[rowMax - rowMin + 1][7];

    // iterate over the month day by day
    PlainDate date = start;
    int column; // zero-based
    int row; // zero-based

    do {
        column = date.getDayOfWeek().getValue(weekmodel) - 1;
        row = date.get(rowmodel.boundedWeekOfMonth()) - rowMin;
        table[row][column] = new Cell(date.getDayOfMonth(), date.isWeekend(locale));
        date = date.plus(1, CalendarUnit.DAYS);
    } while (!date.isAfter(end));

    // HTML output (example)
    System.out.println("<table border=\"1\">");
    System.out.println("<caption>" + month.getDisplayName(locale) + " " + year + "</caption>");

    for (int i = 0; i < table.length; i++) {
        System.out.print("<tr>\n\t");
        for (int j = 0; j < 7; j++) {
            Cell cell = (Cell) table[i][j];
            System.out.print("<td>");
            if (cell != null) {
                if (cell.weekend) {
                    System.out.print("<strong>");
                }
                System.out.print(cell.dayOfMonth);
                if (cell.weekend) {
                    System.out.print("</strong>");
                }
            }
            System.out.print("</td>");
        }
        System.out.print("\n</tr>\n");
    }
    System.out.println("\n</table>");
}

static class Cell {
    final int dayOfMonth;
    final boolean weekend;

    Cell(int dayOfMonth, boolean weekend) {
        this.dayOfMonth = dayOfMonth;
        this.weekend = weekend;
    }
}

示例输出:

<table border="1">
<caption>January 2016</caption>
<tr>
    <td></td><td></td><td></td><td></td><td></td><td>1</td><td><strong>2</strong></td>
</tr>
<tr>
    <td><strong>3</strong></td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td><strong>9</strong></td>
</tr>
<tr>
    <td><strong>10</strong></td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td><strong>16</strong></td>
</tr>
<tr>
    <td><strong>17</strong></td><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td><strong>23</strong></td>
</tr>
<tr>
    <td><strong>24</strong></td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td><strong>30</strong></td>
</tr>
<tr>
    <td><strong>31</strong></td><td></td><td></td><td></td><td></td><td></td><td></td>
</tr>
</table>