WeekCalendarUtils工具类代码,传入起始日期即可返回对应日期的周历日历,年月部分添加周数统计
import java.util.Calendar;
import java.util.Date;
import java.util.Map; import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils; import com.google.common.collect.Maps; /**
* <b>function:</b> 周历
*
* @author hoojo
* @createDate 2016-11-21 上午11:02:08
* @file WeekCalendarUtils.java
* @package
* @project
* @blog http://blog.csdn.net/IBM_hoojo
* @email hoojo_@126.com
* @version 1.0
*/
public abstract class WeekCalendarUtils { public final static String DATE_FORMAT = "yyyy-MM-dd"; private static String getWeekDay(Calendar cal) {
if (cal == null) {
return null;
} switch (cal.get(Calendar.DAY_OF_WEEK)) { case Calendar.MONDAY:
return "星期一";
case Calendar.TUESDAY:
return "星期二";
case Calendar.WEDNESDAY:
return "星期三";
case Calendar.THURSDAY:
return "星期四";
case Calendar.FRIDAY:
return "星期五";
case Calendar.SATURDAY:
return "星期六";
default:
return "星期日";
}
} private static String getSimpleWeekDay(Calendar cal) {
if (cal == null) {
return null;
} switch (cal.get(Calendar.DAY_OF_WEEK)) { case Calendar.MONDAY:
return "一";
case Calendar.TUESDAY:
return "二";
case Calendar.WEDNESDAY:
return "三";
case Calendar.THURSDAY:
return "四";
case Calendar.FRIDAY:
return "五";
case Calendar.SATURDAY:
return "六";
default:
return "日";
}
} public static String[] getWeekDays(boolean hasMonFirstWeekDay) {
if (hasMonFirstWeekDay) {
return new String[] { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };
} else {
return new String[] { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
}
} /**
* <b>function:</b> 获取周历
* @author hoojo
* @createDate 2016-11-21 下午6:00:18
* @param begin 开始日期
* @param end 结束日期
* @return 周历Map
*/
public static Map<Integer, YearModel> get(String begin, String end, boolean hasMonFirstWeekDay) { Map<Integer, YearModel> years = Maps.newLinkedHashMap(); Date beginDate = null;
Date endDate = null; try {
beginDate = DateUtils.parseDate(begin, DATE_FORMAT);
endDate = DateUtils.parseDate(end, DATE_FORMAT); if (beginDate.compareTo(endDate) > 0) {
return null;
} int weekCount = 0, monthWeekCount = 0;
do {
Calendar cal = DateUtils.toCalendar(beginDate);
if (hasMonFirstWeekDay) {
cal.setFirstDayOfWeek(Calendar.MONDAY);
} Map<Integer, MonthModel> months = Maps.newLinkedHashMap();
int year = cal.get(Calendar.YEAR);
YearModel yearModel = null;
if (years.containsKey(year)) {
yearModel = years.get(year);
months = yearModel.getMonths();
} else {
yearModel = new YearModel(year, year + "年", months);
years.put(year, yearModel); weekCount = 0;
} Map<String, WeekModel> weeks = Maps.newLinkedHashMap();
int month = cal.get(Calendar.MONTH) + 1;
MonthModel monthModel = null;
if (months.containsKey(month)) {
monthModel = months.get(month);
weeks = monthModel.getWeeks();
} else {
monthModel = new MonthModel(month, year + "年" + month + "月", weeks);
months.put(month, monthModel); monthWeekCount = 0;
} Map<String, DayModel> days = Maps.newLinkedHashMap();
int weekInMonth = cal.get(Calendar.WEEK_OF_MONTH);
String week = cal.getWeekYear() + "_" + month + "_" + weekInMonth;
if (weeks.containsKey(week)) {
days = weeks.get(week).getDays();
} else {
weeks.put(week, new WeekModel(weekInMonth, month + "月第" + weekInMonth + "周", days)); monthWeekCount++;
weekCount++;
monthModel.setWeekCount(monthWeekCount);
yearModel.setWeekCount(weekCount);
} String weekDay = getWeekDay(cal);
days.put(week + "_" + weekDay, new DayModel(cal.get(Calendar.DAY_OF_MONTH), weekDay, getSimpleWeekDay(cal), beginDate));
/*
System.out.println("日期:" + DateFormatUtils.format(beginDate, DATE_FORMAT));
System.out.println("年份:" + cal.getWeekYear());
System.out.println("月份:" + (cal.get(Calendar.MONTH) + 1));
System.out.println("星期:" + cal.get(Calendar.DAY_OF_WEEK));
System.out.println("本月周次:" + cal.get(Calendar.WEEK_OF_MONTH));
System.out.println();
*/
beginDate = DateUtils.addDays(beginDate, 1);
} while (beginDate.compareTo(endDate) <= 0); } catch (Exception e) {
e.printStackTrace();
}
return years;
} public static Map<Integer, YearModel> get(Date beginDate, Date endDate, boolean hasMonFirstWeekDay) { try {
return get(DateFormatUtils.format(beginDate, DATE_FORMAT), DateFormatUtils.format(endDate, DATE_FORMAT), hasMonFirstWeekDay);
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static class YearModel {
private int yearName;
private String displayName;
private int weekCount;
private Map<Integer, MonthModel> months; public YearModel(int yearName, String displayName, Map<Integer, MonthModel> months) {
super();
this.yearName = yearName;
this.displayName = displayName;
this.months = months;
} public int getYearName() {
return yearName;
}
public void setYearName(int yearName) {
this.yearName = yearName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public Map<Integer, MonthModel> getMonths() {
return months;
}
public void setMonths(Map<Integer, MonthModel> months) {
this.months = months;
} @Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
} public int getWeekCount() {
return weekCount;
} public void setWeekCount(int weekCount) {
this.weekCount = weekCount;
}
} public static class MonthModel { private int monthName;
private String displayName;
private int weekCount;
private Map<String, WeekModel> weeks; public MonthModel(int monthName, String displayName, Map<String, WeekModel> weeks) {
super();
this.monthName = monthName;
this.displayName = displayName;
this.weeks = weeks;
} public int getMonthName() {
return monthName;
}
public void setMonthName(int monthName) {
this.monthName = monthName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public Map<String, WeekModel> getWeeks() {
return weeks;
}
public void setWeeks(Map<String, WeekModel> weeks) {
this.weeks = weeks;
}
public int getWeekCount() {
return weekCount;
} public void setWeekCount(int weekCount) {
this.weekCount = weekCount;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
}
} public static class WeekModel { private int weekName;
private String displayName;
private Map<String, DayModel> days; public WeekModel(int weekName, String displayName, Map<String, DayModel> days) {
super();
this.weekName = weekName;
this.displayName = displayName;
this.days = days;
}
public int getWeekName() {
return weekName;
}
public void setWeekName(int weekName) {
this.weekName = weekName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public Map<String, DayModel> getDays() {
return days;
}
public void setDays(Map<String, DayModel> days) {
this.days = days;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
}
} public static class DayModel { private int dayName;
private String displayName;
private String simpleName;
private Date date; public DayModel(int dayName, String displayName, String simpleName, Date date) {
super();
this.dayName = dayName;
this.displayName = displayName;
this.simpleName = simpleName;
this.date = date;
}
public int getDayName() {
return dayName;
}
public void setDayName(int dayName) {
this.dayName = dayName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getSimpleName() {
return simpleName;
}
public void setSimpleName(String simpleName) {
this.simpleName = simpleName;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SIMPLE_STYLE);
}
} public static void main(String[] args) {
System.out.println(get("2016-06-01", "2017-07-03", false));
}
}
一个table页面展示部分
<style type="text/css">
td {
border: 1px solid black;
background-color: #eeeeee;
padding: 5px;
text-align: center;
} table {
border-collapse: collapse;
border-spacing: 5px;
border: 1px solid black;
} th {
border: 1px solid black;
background: #9DACBF;
color: #FFF;
height: 20px;
line-height: 20px
} body {
font-family: "宋体", "Arial", "Helvetica";
font-size: 12px;
font-style: normal;
font-weight: lighter;
} .head {
background-color: #ccc;
font-weight: bold;
} .head b {
color: #337ab7;
} .odd td {
background-color: white;
} .even td {
background-color: lavender;
}
</style> <table class="xuenianTable" width="100%" cellspacing="0" cellpadding="0" border="0">
<thead>
<tr height="55">
<th colspan="10" style="font-size: 28px;"> (${param.fileName })教学周历</th>
</tr>
<tr height="35">
<th width="10%">年份</th>
<th width="10%">月份</th>
<th width="10%">周次</th>
<th width="10%">一</th>
<th width="10%">二</th>
<th width="10%">三</th>
<th width="10%">四</th>
<th width="10%">五</th>
<th width="10%" style="color: #f34747;">六</th>
<th width="10%" style="color: #f34747;">七</th>
</tr>
</thead> <tbody> <c:set var="weekCount" value="1"/>
<c:forEach items="${result }" varStatus="st" var="year">
<c:set var="yearFirst" value="true"/>
<c:forEach items="${year.value.months }" var="month">
<c:set var="monthFirst" value="true"/>
<c:forEach items="${month.value.weeks }" var="week">
<tr class="zhuanjafora ${st.index % 2 == 0 ? 'odd' : 'even' }">
<c:if test="${yearFirst }">
<c:set var="yearFirst" value="false"/>
<td rowspan="${year.value.weekCount }" title="${year.value.displayName }">${year.value.displayName }</td>
</c:if>
<c:if test="${!monthFirst}">
<c:set var="weekCount" value="${weekCount + 1 }"/>
</c:if>
<c:if test="${monthFirst }">
<c:set var="monthFirst" value="false"/>
<td rowspan="${month.value.weekCount }" title="${month.value.displayName }">${month.value.monthName }月</td>
</c:if>
<td title="${week.value.displayName }">${weekCount }周</td>
<c:forEach items="${weekDays }" var="weekDay">
<c:set var="weekDayKey" value="${week.key}_${weekDay }"/>
<td title='${week.value.days[weekDayKey].displayName } <fmt:formatDate value="${week.value.days[weekDayKey].date }" pattern="yyyy-MM-dd"/>' style="color: ${weekDay == '星期六' or weekDay == '星期日' ? 'red' : ''};">
${week.value.days[weekDayKey].dayName }
</td>
</c:forEach>
</tr>
</c:forEach>
</c:forEach>
</c:forEach>
</tbody>
</table>
日历形式展示部分,在日历中一个周次不足6周会用空白格填充,来保证布局完整不错位。
<div style="width: 100%; height: 490px; overflow: scroll;"> <c:set var="weekCount" value="1"/>
<c:forEach items="${result }" var="year">
<c:set var="yearFirst" value="true"/>
<c:forEach items="${year.value.months }" var="month" varStatus="st">
<c:set var="monthFirst" value="true"/>
<div style="width: 400px; height: 230px; float: left; margin-bottom: 5px;">
<table border="0" cellspacing="0" cellpadding="0" width="100%" class="zhouli">
<thead>
<tr height="35">
<th width="10%">年份</th>
<th width="10%">月份</th>
<th width="10%">周次</th>
<th width="10%">一</th>
<th width="10%">二</th>
<th width="10%">三</th>
<th width="10%">四</th>
<th width="10%">五</th>
<th width="10%" style="color: #f34747;">六</th>
<th width="10%" style="color: #f34747;">七</th>
</tr>
</thead> <tbody>
<c:set var="fillWeekCount" value="${6 - fn:length(month.value.weeks) }"/>
<c:forEach items="${month.value.weeks }" var="week">
<tr class="zhuanjafora ${st.index % 2 == 0 ? 'odd' : 'even' }">
<c:if test="${!monthFirst}">
<c:set var="weekCount" value="${weekCount + 1 }"/>
</c:if>
<c:if test="${monthFirst }">
<c:set var="monthFirst" value="false"/>
<td rowspan="${month.value.weekCount + fillWeekCount }" title="${year.value.displayName }">${year.value.yearName }</td>
<td rowspan="${month.value.weekCount + fillWeekCount }" title="${month.value.displayName }">${month.value.monthName }月</td>
</c:if>
<td title="${week.value.displayName }">${weekCount }周</td>
<c:forEach items="${weekDays }" var="weekDay">
<c:set var="weekDayKey" value="${week.key}_${weekDay }"/>
<td title='${week.value.days[weekDayKey].displayName } <fmt:formatDate value="${week.value.days[weekDayKey].date }" pattern="yyyy-MM-dd"/>' style="color: ${weekDay == '星期六' or weekDay == '星期日' ? 'red' : ''};">
${week.value.days[weekDayKey].dayName }
</td>
</c:forEach>
</tr>
</c:forEach> <c:forEach begin="1" end="${fillWeekCount }" var="i">
<tr class="zhuanjafora ${st.index % 2 == 0 ? 'odd' : 'even' }">
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</c:forEach>
</c:forEach> </div>