Java 周历日历
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>
Java 周历日历的更多相关文章
- 《手把手教你》系列技巧篇(三十七)-java+ selenium自动化测试-日历时间控件-上篇(详解教程)
1.简介 我们在实际工作中,有可能遇到有些web产品,网页上有一些时间选择,然后支持按照不同时间段范围去筛选数据.网页上日历控件一般,是一个文本输入框,鼠标点击,就会弹出日历界面,可以选择具体日期.这 ...
- 《手把手教你》系列技巧篇(三十八)-java+ selenium自动化测试-日历时间控件-下篇(详解教程)
1.简介 理想很丰满现实很骨感,在应用selenium实现web自动化时,经常会遇到处理日期控件点击问题,手工很简单,可以一个个点击日期控件选择需要的日期,但自动化执行过程中,完全复制手工这样的操作就 ...
- 用java代码作日历
import java.util.Calendar; public class CalendarBean { String day[]; int year=2005,month=0; public v ...
- Java实现打印日历的功能
编写一个程序,显示给定年月的日历.程序提示用户输入年份和月份,然后显示该月的整个日历. 代码: import java.util.Scanner; public class PrintCalendar ...
- Java基础10-循日历制作
编写过程:先指定固定的年份来输出指定月份的日历 /*计算1900年1月1日到2018年11月1日一共有多少天 计算月份 对7进行取模,结果就为星期几 */ import java.util.Scann ...
- java代码(生成日历时间)
package test; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; p ...
- 用JAVA写一个日历计划
效果图(自己需要在前台加css修饰)
- Java编写的日历,输入年月,输出这个月的日期与星期
import java.util.Scanner; public class rili { public static void main(String[] args) { for (int g = ...
- java编写本月日历
代码如下: import java.time.*; public class Main { public static void main(String arg[]){ LocalDate date ...
随机推荐
- Eclipse报错:Setting property 'source' to 'org.eclipse.jst.jee.server:test1' did no
最近把Eclipse的maven插件从m2eclipse更新到m2e后出了一些莫名其妙的的问题.今天又出了一个,就是Eclipse新建的Maven Web project在tomcat里启动后报错,具 ...
- sed初学
1.连接多个sed命令,使用;号 2.批量修改文本中匹配到相应字符串的行 例如,将文本中log_server_port = "12345" 的12345修改成变量中存储的值 sed ...
- handlebars
Handlebars 是 JavaScript 一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less template"(无逻辑模版)的 ...
- html添加网络音乐
IE浏览器,其他的不一定适应 <embed src="http://www.kmfhsj.com/fish-photo/music/xiaochenggushi.mp3" a ...
- easyui combobox onSelect事件
easyui combobox 没有onchange事件,只有onSelect事件 1 $(function () { $('#Select6').combobox({ onSelect: funct ...
- Windows 8.1 应用再出发 (WinJS) - 几种新增控件(1)
Windows 8.1 和 WinJS 引入了以下新控件和功能,分别是:AppBarCommand.BackButton.Hub.ItemContainer.NavBar.Repeater.WebVi ...
- NOIP 2015 信息传递
kawayi 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的 ...
- MVVM了解
了解WPF要有两年,零零碎碎也做了几个项目,之前面试的时候面试官必问对MVVM的了解. 其实不太了解,只是做项目的时候一直采用这种模式,Model-View-ViewModel.以下是我在了解过程中的 ...
- ubuntu 挂载新硬盘
http://www.cnblogs.com/hnrainll/archive/2012/02/27/2369331.html
- 使用java 程序创建格式为utf-8文件的方法(写入和读取json文件)
使用java 程序创建格式为utf-8文件的方法: try{ File file=new File("C:/11.jsp"); ...