答案 0 :(得分:1)
我提供一个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>