主要就是利用 java的Calendar 类
传入一个 参数 如yyyyMMdd 20101201 这样的参数
然后找到 月 的第一天 和 月的最后一天 进行循环。
将循环放到 星期数组里面 传到页面。
private String currentDay;
private String currentMonth;
private List<String[]> weeksOfMonth;
/**
* 分页.查询.排序.
*/
public String listSchedule() throws Exception {
Calendar now = Calendar.getInstance();
if (currentDay != null) {
try {
now.setTime(new SimpleDateFormat("yyyyMMdd")
.parse(currentDay));
} catch (Exception e) {
}
}
Calendar dateFirst = Calendar.getInstance();
dateFirst.set(Calendar.YEAR, now.get(Calendar.YEAR));
dateFirst.set(Calendar.MONTH, now.get(Calendar.MONTH));
dateFirst.set(Calendar.DATE, 1);
//将当前天和当前月设置值
currentDay = new SimpleDateFormat("yyyyMMdd").format(now.getTime());
currentMonth = new SimpleDateFormat("yyyyMM").format(now.getTime());
List<String> daysOfMonth = new ArrayList<String>();// 变量放日期
weeksOfMonth = new ArrayList<String[]>();// 转换成的星期
for (int i = 1; i < ((dateFirst.get(Calendar.DAY_OF_WEEK)+5)%7); i++) {// 添加1号前面的空格
daysOfMonth.add("");
}
for (int i = 1; i <= now.getActualMaximum(Calendar.DATE); i++) {
//now.getActualMaximum(Calendar.DATE)获得当月天数
daysOfMonth.add(i + "");// 添加月下面的日期
}
for (int i = daysOfMonth.size(); i < 6 * 7; i++) {
daysOfMonth.add("");// 添加月末后面的空格
}
for (int j = 0; j < daysOfMonth.size(); j = j + 7) {// 将数组转换成星期放到list里面
String[] week = new String[7];
int index = 0;
week[index] = daysOfMonth.get(j + index++);// 星期一
week[index] = daysOfMonth.get(j + index++);// 星期二
week[index] = daysOfMonth.get(j + index++);// 星期三
week[index] = daysOfMonth.get(j + index++);// 星期四
week[index] = daysOfMonth.get(j + index++);// 星期五
week[index] = daysOfMonth.get(j + index++);// 星期六
week[index] = daysOfMonth.get(j + index++);// 星期日
weeksOfMonth.add(week);
}
return SUCCESS;
}
这里有个问题
(dateFirst.get(Calendar.DAY_OF_WEEK)+5)%7)
本来 用
DAY_OF_WEEK
就可以获得星期,可是星期是从 周日开始的。想从周一开始就得 向后挪 5
在页面显示
<s:iterator value="weeksOfMonth" status="status" id="week">
<fs:gridPanelDataTable4Date index="${status.index}">
<fs:gridPanelDataColumn4Date width="6" height="40"> </fs:gridPanelDataColumn4Date>
<s:iterator begin="0" end="6" step="1" id="index"><!-- 进行星期的7天循环 -->
<s:if test="${index} == 5 || ${index} == 6"><!-- 判断是否是周六,周日换背景为黄色 -->
<fs:gridPanelDataColumn4Date width="100" height="40" style="background-color: #ffffcc;">
<span class="scheduleDateWeekendDiv"><s:property value="${week[index]}"/></span>
<s:if test="${week[index]} != ''">
<a href="javaScript:showScheduleViewDiv('${currentMonth}<s:if test="${week[index]} < 10 ">0${week[index]}</s:if><s:else>${week[index]}</s:else>');">编辑</a>
<a href="${ctx}/addUserInfo.action?id=<s:property value="id"/>">查看</a>
</s:if>
</fs:gridPanelDataColumn4Date>
</s:if>
<s:else>
<fs:gridPanelDataColumn4Date width="100" height="40">
<span class="scheduleDateWeekendDiv"><s:property value="${week[index]}"/></span>
<s:if test="${week[index]} != ''">
<a href="javaScript:showScheduleViewDiv('${currentMonth}<s:if test="${week[index]} < 10 ">0${week[index]}</s:if><s:else>${week[index]}</s:else>');">编辑</a>
<a href="${ctx}/addUserInfo.action?id=<s:property value="id"/>">查看</a>
</s:if>
</fs:gridPanelDataColumn4Date>
</s:else>
</s:iterator>
</fs:gridPanelDataTable4Date>
</s:iterator>
其中用到了一些自己封装的 标签。
效果图:
上月 下月的 action
/**
* 上一月
*/
public String prevMonthSchedule() throws Exception {
Calendar now = Calendar.getInstance();
if (currentDay != null) {
try {
now.setTime(new SimpleDateFormat("yyyyMMdd")
.parse(currentDay));
} catch (Exception e) {
}
}
now.set(Calendar.MONTH, now.get(Calendar.MONTH) - 1);
currentDay = new SimpleDateFormat("yyyyMMdd").format(now.getTime());
System.out.println("currentDay:" + currentDay);
return SUCCESS;
}
/**
* 下一月
*/
public String nextMonthSchedule() throws Exception {
Calendar now = Calendar.getInstance();
if (currentDay != null) {
try {
now.setTime(new SimpleDateFormat("yyyyMMdd")
.parse(currentDay));
} catch (Exception e) {
}
}
now.set(Calendar.MONTH, now.get(Calendar.MONTH) + 1);
currentDay = new SimpleDateFormat("yyyyMMdd").format(now.getTime());
System.out.println("currentDay:" + currentDay);
return SUCCESS;
}
源代码 和 oa 放到一起了
demo:
http://freeweboa.appspot.com/index.html
里面的日程管理
下载地址:
http://code.google.com/p/freewebsys/downloads/list