欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java编写全年考勤日历

程序员文章站 2024-03-01 13:34:16
本文实例为大家分享了java编写全年考勤日历的具体代码,供大家参考,具体内容如下 java代码: package com.wp.action; im...

本文实例为大家分享了java编写全年考勤日历的具体代码,供大家参考,具体内容如下

java代码:

package com.wp.action;
 
import java.text.simpledateformat;
import java.util.arraylist;
import java.util.calendar;
import java.util.hashmap;
 
public class calendaraction extends mainaction {
 
 private static final long serialversionuid = 1l;
 
 private int maxcols;
 private string html;
 private string clickdate;
 
 public string getclickdate() {
 return clickdate;
 }
 
 public void setclickdate(string clickdate) {
 this.clickdate = clickdate;
 }
 
 public string init() {
 calendar cal = calendar.getinstance();
 int month = cal.get(calendar.month) + 1;
 initmaxcols();
 html = createtbl();
 
 return success;
 }
 
 private void initmaxcols() {
 // 每行开头灰色的格数
 int headdisableddays;
 // 当月的天数
 int onemonthdays;
 calendar cal = calendar.getinstance();
 cal.set(calendar.day_of_month, 1);
 
 for (int i = 0; i < 12; i++) {
 
  if (cal.get(calendar.day_of_week) == calendar.sunday) {
  // 周日空六格
  headdisableddays = 6;
  } else {
  headdisableddays = cal.get(calendar.day_of_week)
   - calendar.monday;
  }
 
  onemonthdays = cal.getactualmaximum(calendar.day_of_month);
  if (headdisableddays + onemonthdays > maxcols) {
  maxcols = headdisableddays + onemonthdays;
  }
  cal.add(calendar.month, 1);
 }
 
 }
 
 private string createtbl() {
 stringbuffer html = new stringbuffer();
 string[] weekdays = { "一", "二", "三", "四", "五", "六", "日" };
 simpledateformat formattd = new simpledateformat("yyyymmdd");
 simpledateformat formatheader = new simpledateformat("yyyy年mm月");
 simpledateformat formattitle = new simpledateformat("yyyy年mm月dd日");
 hashmap<string, string> map = getcalendardetail();
 
 // 每行开头灰色的格数
 int headdisableddays;
 
 // html.append("<table id='caltbl'>\r\n");
 html.append("<tr>\r\n");
 html.append("<th></th>\r\n");
 for (int col = 0; col < maxcols; col++) {
  html.append("<th>");
  html.append(weekdays[col % weekdays.length]);
  html.append("</th>\r\n");
 }
 html.append("</tr>\r\n");
 calendar cal = calendar.getinstance();
 int month = cal.get(calendar.month);
 for (int months = 0; months < 12; months++) {
  html.append("<tr>\r\n");
  string s;
  s = formatheader.format(cal.gettime());
  html.append("<td class='rowheader'>" + s + "</td>\r\n");
  
  cal.set(calendar.day_of_month, 1);
  if (cal.get(calendar.day_of_week) == calendar.sunday) {
  // 周日空六格
  headdisableddays = 6;
  } else {
  headdisableddays = cal.get(calendar.day_of_week)
   - calendar.monday;
  }
  cal.add(calendar.day_of_month, -headdisableddays);
 
  for (int col = 0; col < maxcols; col++) {
 
  html.append("<td id='");
 
  string date = formattd.format(cal.gettime());
  html.append(date + "' ");
  // if (headdisableddays-- > 0) {
  // html.append("class='disabledtd'");
  // }else
  if (month != cal.get(calendar.month)) {
   html.append("class='disabledtd'");
  
  } else if (map.containskey(formattd.format(cal.gettime()))) {
   int type = integer.parseint(map.get(formattd.format(cal
    .gettime())));
   if(type == 1){
   //html.append("class='holidaytd'");
   }else if(type == 2){
   html.append("class='holidaytd'");
   }
  } else if (cal.get(calendar.day_of_week) == calendar.saturday
   || cal.get(calendar.day_of_week) == calendar.sunday) {
   html.append("class='weekendtd'");
  } else {
   html.append("class='generaltd'");
  }
  html.append(" title='" + formattitle.format(cal.gettime())
   + "'");
  html.append(">");
 
  html.append(cal.get(calendar.day_of_month));
  html.append("</td>\r\n");
  cal.add(calendar.day_of_month, 1);
 
  }
 
  html.append("</tr>\r\n");
  if (month == cal.get(calendar.month)) {
  cal.add(calendar.month, 1);
  }
  month = cal.get(calendar.month);
 }
 // html.append("</table>\r\n");
 return html.tostring();
 }
 
 public string gethtml() {
 return html;
 }
 
 public void sethtml(string html) {
 this.html = html;
 }
 
 private hashmap<string, string> getcalendardetail() {
 hashmap<string, string> map;
 map = new hashmap<string, string>();
 map.put("20150404", "2");
 map.put("20150405", "2");
 map.put("20150406", "2");
 map.put("20150501", "2");
 map.put("20150502", "2");
 map.put("20150503", "2");
 map.put("20150622", "2");
 map.put("20151001", "2");
 map.put("20151002", "2");
 map.put("20151003", "2");
 
 return map;
 }
 public string datecellclick(){
 
 return success;
 }
}

action配置:

<action name="calendar" class="com.wp.action.calendaraction" method="init">
   <result name="success" type="json"></result>
</action>

html代码:

<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<%@ page language="java" contenttype="text/html; charset=utf-8"
 pageencoding="utf-8"%>
<%
 string path = request.getcontextpath();
 string basepath = request.getscheme() + "://"
  + request.getservername() + ":" + request.getserverport()
  + path;
%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
 <style type="text/css">
  .disabledtd{
  background-color:gray;
  }
  .weekendtd{
  background-color:yellow;
  }
  .holidaytd{
  background-color:green;
  }
  .generaltd{
  background-color:white;
  }
  #caltbl{
  font-family: verdana,arial,sans-serif;
  font-size:13px;
  color:#333333;
  border-width: 1px;
  border-color: #a9c6c9;
  border-collapse: collapse;
  }
  #caltbl th{
  border-width: 1px;
  padding: 4px;
  border-style: solid;
  border-color: #a9c6c9;
  background-color:olive;
  }
  #caltbl td {
  border-width: 1px;
  padding: 4px;
  border-style: solid;
  border-color: #a9c6c9;
  }
  .rowheader{
  background-color:olive;
  }
 </style>
 
 <head>
 
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
 <title>login page</title>
 <link rel="stylesheet" type="text/css" media="screen"
  href="<%=basepath%>/html/styles/styles.css" rel="external nofollow" />
 <script src="<%=basepath%>/html/scripts/common.js"
  type="text/javascript"></script>
 <script src="<%=basepath%>/html/scripts/jquery.js"
  type="text/javascript"></script>
 <script src="<%=basepath%>/html/scripts/jquery.json-2.2.min.js"
  type="text/javascript"></script>
 <script src="<%=basepath%>/html/scripts/jquery.ui.custom.js"
  type="text/javascript"></script>
 <script src="<%=basepath%>/html/scripts/script.js"
  type="text/javascript"></script>
  <script type="text/javascript">
  $(document).ready(function(){
  var checktype = 0;
  $.post('calendar',{},function(data,status)
  {
   if(data != null && data.html != null && data.html != ""){
   $("#caltbl").html( data.html);
   $("#caltbl td").click(tdclick);
   $("#checktype input").click(typecheck);
   }
  }).error(function(){
   
  });
 
  var tdclick = function(){
  
   if(this.classname == 'rowheader' || this.classname == 'disabledtd'){
   return;
   }
   if(checktype != null && checktype != "" && checktype != 0){
   if(checktype == 1){   
    this.style.backgroundcolor="white";
   }else if(checktype == 2){    
    this.style.backgroundcolor="yellow";
   }else if(checktype == 3){    
    this.style.backgroundcolor="green";
   }
   }
  };
  var typecheck = function(){
   checktype = this.value;
  };
  
  
  });
  
 </script>
 </head>
 <body>
 <div id="calendar">
  <table id="caltbl"></table>  
 </div>
 <div>
  <table id="checktype">
  <tr>
   <td style="background-color: white;"> <input type="radio" name="type" value="1"> </td>
   <td style="background-color: yellow;"><input type="radio" name="type" value="2"></td>
   <td style="background-color: green;"> <input type="radio" name="type" value="3"></td>
  </tr>
  </table>
 </div> 
 </body>
</html>

效果如下

java编写全年考勤日历

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。