2

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

我怎样才能做到?

像这样

4

1 回答 1

2

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

关于假期:如果您已经定义了每年的假期列表,那么您可以使用类似的表达式来询问日期是否为假期boolean isHoliday = holidayList.contains(date)。顺便说一句,最新版本的 Time4J (v3.16/v4.13) 也支持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>
于 2016-03-06T21:52:54.437 回答