我使用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>