I use library Time4j for Java and I selected some days as holydays and saved in a variables. Now I want to display all days of a year something like on the picture but with my holydays highlighted.
How can I make it?
I present a Time4J-example for one month only which can easily be applied on every month of a full calendar year. Holidays are not explicitly supported by Time4J, but I have presented a simple HTML-example how to highlight special days, here weekends.
About holidays: If you have defined a list of holidays per year then you can ask a date to be a holiday or not by using an expression like boolean isHoliday = holidayList.contains(date)
. By the way, the newest version of Time4J (v3.16/v4.13) also supports computation of Easter, so the computation of any holidays per year should not be so difficult in Western locales.
Updated to realize a localized start of week which requires the usage of two week models:
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;
}
}
Example output:
<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>