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 ...
随机推荐
- SAS文档:简单的随机点名器
本次实验,我们设计了一个简单的随机点名系统,下面我来介绍一下它的SRS文档. 1.功能需求: 1.1 模块1 在此模块中,我们设置了RandomName类,创建一个随机点名器,里面加入了所在课程的名单 ...
- 尽量少用if else
Michael Feathers是Object Mentor International公司的技术顾问.他的工作不仅是技术开发,他还参与对世界各地技术团队进行培训.指导等工作.他曾开发了将JUnit迁 ...
- HP 7440老机器重启
一大早内存就报内存100% 处理流程 1.kmeminfo -u | more ,找出内存占用过大的进程ID --------------------------------------------- ...
- jsp页面不能使用EL表达式
在页面中添加 <%@ page isELIgnored = "flase" %>
- 不要轻易delete void*指针,这样会隐藏比较多的错误。
#include<iostream> using namespace std; class Object{ void* data; const int size; const char i ...
- SQL Server 建表语句
IF EXISTS(SELECT * FROM sys.Tables WHERE name='stu_info') DROP TABLE stu_infoGoCreate table stu_inf ...
- Exception in thread “main” com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String
String json="A valid json"; Job job = new Gson().fromJson(json, Job.class); Exception in t ...
- IOS学习笔记之关键词@dynamic
IOS学习笔记之关键词@dynamic @dynamic这个关键词,通常是用不到的. 它与@synthesize的区别在于: 使用@synthesize编译器会确实的产生getter和setter方法 ...
- Log4j 配置数据库连接池(将日志信息保存到数据库)
org.apache.log4j.jdbc.JDBCAppender 是利用传统的 JDBC 连接方法,这种方式连接数据库效率低下,为了解决这个问题,现在自定义一个 Log4j 的 Appender, ...
- 设计模式之美:Command(命令)
索引 别名 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):直接注入 Receiver 对象,Command 决定调用哪个方法. 实现方式(二):注入 Receiver 的指定方法, ...