Java 设定工作日日历

开发清单

  • JDK 1.7
  • Servlet 3.0
  • JSP
  • Dom4j 1.6.1
  • FullCanlendar 3

后台考虑兼容性,并没有使用数据库,而是采用xml格式存储数据,毕竟一年只有365天,点击FullCanlendar的某一天,设置为休息日,再次点击取消。

XML格式为:

<?xml version="1.0" encoding="UTF-8"?>

<roots xmlns="http://www.phpStudy.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.phpStudy.net note.xsd">
  <event ID="S2016-12-11" start="2016-12-11" title="休息日"/>
  <event ID="S2016-12-04" start="2016-12-04" title="休息日"/>
  <event ID="S2016-12-03" start="2016-12-03" title="休息日"/>
  <event ID="S2016-12-10" start="2016-12-10" title="休息日"/>
  <event ID="S2016-12-17" start="2016-12-17" title="休息日"/>
  <event ID="S2016-12-18" start="2016-12-18" title="休息日"/>
  <event ID="S2016-12-24" start="2016-12-24" title="休息日"/>
  <event ID="S2016-12-25" start="2016-12-25" title="休息日"/>
  <event ID="S2016-12-31" start="2016-12-31" title="休息日"/>
  <event ID="S2017-01-01" start="2017-01-01" title="休息日"/>
</roots>

约束文档为:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.phpStudy.net"
xmlns="http://www.phpStudy.net"
elementFormDefault="qualified">

<xs:element name="roots">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="event" minOccurs="0" maxOccurs="366" >
            <xs:complexType>
                <xs:attribute name="ID" type="xs:ID" />
                <xs:attribute name="title" type="xs:string" />
            </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
</xs:element>

</xs:schema>

整个项目只使用了一个index.html作为展示界面。

具体代码为:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>wday政府日历</title>

    <!-- Bootstrap -->
    <!-- 新 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">

    <!-- 可选的Bootstrap主题文件(一般不用引入) -->
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn‘t work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="http://cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="http://cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
    <link href=‘./baseCanlendar/fullcalendar.min.css‘ rel=‘stylesheet‘ />
    <link href=‘./baseCanlendar/fullcalendar.print.min.css‘ rel=‘stylesheet‘ media=‘print‘ />
    <script src=‘./lib/jquery.min.js‘></script>
<script>

    var cal={};

    $(document).ready(function() {

        var arrData = [];
        $.ajax({
            url: "./FullCanlendarServlet",
            async:false,
            type:"GET",
            success: function(data){
                arrData = data;
          }
        });

        cal = $(‘#calendar‘).fullCalendar({
            header: {
                left: ‘prev,next today‘,
                center: ‘title‘,
                right: ‘prevYear,nextYear‘
            },
            editable: false,
            droppable: false, // this allows things to be dropped onto the calendar
            eventAfterAllRender: function( view ) {
                //alert(‘好吧,全部加载完毕‘);
            },
            dayClick: function( date, jsEvent, view) {
                var year = date._d.getFullYear();
                var month = date._d.getMonth()+1;
                var day = date._d.getDate();
                if (month<10){
                    month = "0"+month;
                }
                if (day<10){
                    day = "0"+day;
                }
                var d =year+"-"+month+"-"+day; 

                $.ajax({
                    url: "./FullCanlendarSaveTag",
                    async:false,
                    type:"GET",
                    data:{start:d},
                    beforeSend:function (){
                        showModal();
                    },
                    success: function(data){

                        if (data.success===‘sav‘){
                            $(‘#calendar‘).fullCalendar(‘addEventSource‘,[{
                                start:d,
                                title: ‘休息日‘,
                                id:‘S‘+d

                            }]);
                            hideModal();
                        }else{
                            alert("已经存在");
                            hideModal();
                        }
                  }
                });
            },
            events: arrData,
            eventClick:function( event, jsEvent, view ) {
                var d =event.id; 

                $.ajax({
                    url: "./FullCanlendarSaveTag",
                    async:false,
                    type:"POST",
                    data:{id:d},
                    beforeSend:function (){
                        showModal();
                    },
                    success: function(data){
                        if(data.success===‘del‘){
                            cal.fullCalendar("removeEvents", event.id);
                            hideModal();
                        }else{
                            alert("不存在");
                            hideModal();
                        }
                  }
                });
            }
        });
    });

    function hideModal(){
        console.log("$(‘#myModal‘).modal(‘hide‘);");
        $(‘#myModal‘).modal(‘hide‘);
    }

    function showModal(){
        console.log("$(‘#myModal‘).modal({backdrop:‘static‘,keyboard:false});");
        $(‘#myModal‘).modal({backdrop:‘static‘,keyboard:false});
    }

    function showUserHoliday(){
        $(‘#userHolidayModal‘).modal(‘show‘);
    }

    //设置自定义标签
    function setUserHolidayTag(){

        var tagText = $(‘#userHolidayText‘).val();

        $(‘#userHolidayTag‘).html(tagText);

        $(‘#userHolidayModal‘).modal(‘hide‘);

        setDraggable();
    }

    function initCalendar(){
        if(window.confirm(‘确定初始化?‘)){
            var moment = cal.fullCalendar(‘getDate‘);
            var y = moment.format();
            var sp = y.split("-");

            $.ajax({
                url: "./FullCanlendarInit",
                async:false,
                type:"POST",
                data:{year:sp[0]},
                beforeSend:function (){
                    showModal();
                },
                success: function(data){
                    if(data.success===‘init‘){
                        alert("init ok");
                        hideModal();
                    }else{
                        alert("init error");
                        hideModal();
                    }
              }
            });

            window.location.reload();
        }
    }

    function clearCalendar(){
        if(window.confirm(‘确定清空?‘)){
            $.ajax({
                url: "./FullCanlendarInit",
                async:false,
                type:"GET",
                beforeSend:function (){
                    showModal();
                },
                success: function(data){
                    if(data.success===‘clear‘){
                        alert("clear ok");
                        hideModal();
                    }else{
                        alert("clear error");
                        hideModal();
                    }

              }
            });

            cal.fullCalendar(‘removeEvents‘);
        }
    }

</script>
<style>

    body {
        margin-top: 40px;
        text-align: center;
        font-size: 14px;
        font-family: "Lucida Grande",Helvetica,Arial,Verdana,sans-serif;
    }

    #wrap {
        width: 1100px;
        margin: 0 auto;
    }

    #external-events {
        float: left;
        width: 150px;
        padding: 0 10px;
        border: 1px solid #ccc;
        background: #eee;
        text-align: left;
    }

    #external-events h4 {
        font-size: 16px;
        margin-top: 0;
        padding-top: 1em;
    }

    #external-events .fc-event {
        margin: 10px 0;
        cursor: pointer;
    }

    #external-events p {
        margin: 1.5em 0;
        font-size: 11px;
        color: #666;
    }

    #external-events p input {
        margin: 0;
        vertical-align: middle;
    }

    #calendar {
        width: 900px;
    }

</style>
</head>
<body style="background-color: #C7EDCC;">
    <button type="button" class="btn btn-primary" onclick="clearCalendar();">清空工作日</button>
    <button type="button" class="btn btn-success" onclick="initCalendar();">初始化工作日</button>
    <div style="">
        <div id=‘calendar‘></div>
    </div>
    <div class="modal fade" id="myModal">
      <div class="modal-dialog modal-sm">

          <img alt="" src="data:image/gif;base64,R0lGODlhGQAZAJECAK7PTQBjpv///wAAACH/C05FVFNDQVBFMi4wAwEAAAAh/wtYTVAgRGF0YVhNUDw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNS1jMDE0IDc5LjE1MTQ4MSwgMjAxMy8wMy8xMy0xMjowOToxNSAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDo5OTYyNTQ4Ni02ZGVkLTI2NDUtODEwMy1kN2M4ODE4OWMxMTQiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6RUNGNUFGRUFGREFCMTFFM0FCNzVDRjQ1QzI4QjFBNjgiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6RUNGNUFGRTlGREFCMTFFM0FCNzVDRjQ1QzI4QjFBNjgiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENDIChXaW5kb3dzKSI+IDx4bXBNTTpEZXJpdmVkRnJvbSBzdFJlZjppbnN0YW5jZUlEPSJ4bXAuaWlkOjk5NjI1NDg2LTZkZWQtMjY0NS04MTAzLWQ3Yzg4MTg5YzExNCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo5OTYyNTQ4Ni02ZGVkLTI2NDUtODEwMy1kN2M4ODE4OWMxMTQiLz4gPC9yZGY6RGVzY3JpcHRpb24+IDwvcmRmOlJERj4gPC94OnhtcG1ldGE+IDw/eHBhY2tldCBlbmQ9InIiPz4B//79/Pv6+fj39vX08/Lx8O/u7ezr6uno5+bl5OPi4eDf3t3c29rZ2NfW1dTT0tHQz87NzMvKycjHxsXEw8LBwL++vby7urm4t7a1tLOysbCvrq2sq6qpqKempaSjoqGgn56dnJuamZiXlpWUk5KRkI+OjYyLiomIh4aFhIOCgYB/fn18e3p5eHd2dXRzcnFwb25tbGtqaWhnZmVkY2JhYF9eXVxbWllYV1ZVVFNSUVBPTk1MS0pJSEdGRURDQkFAPz49PDs6OTg3NjU0MzIxMC8uLSwrKikoJyYlJCMiISAfHh0cGxoZGBcWFRQTEhEQDw4NDAsKCQgHBgUEAwIBAAAh+QQFCgACACwAAAAAGQAZAAACTpSPqcu9AKMUodqLpAb0+rxFnWeBIUdixwmNqRm6JLzJ38raqsGiaUXT6EqO4uIHRAYQyiHw0GxCkc7l9FdlUqWGKPX64mbFXqzxjDYWAAAh+QQFCgACACwCAAIAFQAKAAACHYyPAsuNH1SbME1ajbwra854Edh5GyeeV0oCLFkAACH5BAUKAAIALA0AAgAKABUAAAIUjI+py+0PYxO0WoCz3rz7D4bi+BUAIfkEBQoAAgAsAgANABUACgAAAh2EjxLLjQ9UmzBNWo28K2vOeBHYeRsnnldKBixZAAA7" />

      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

    <div class="modal fade" id="userHolidayModal">
      <div class="modal-dialog modal-sm">
        <div class="modal-content">
          <div class="modal-body">
            <input type="text" id="userHolidayText" />
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-primary" onclick="setUserHolidayTag()">设置</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    <script src=‘./lib/moment.min.js‘></script>
    <script src=‘./lib/jquery-ui.min.js‘></script>
    <script src=‘./baseCanlendar/fullcalendar.min.js‘></script>
    <script src=‘./baseCanlendar/zh-cn.js‘></script>
    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
</body>
</html>

服务端使用Java Servlet处理。功能包括,【初始化日历】,【清空日历】,【点击日历增加休息日标签】,【点击标签移除标签】。

两点功能,采用dom4j为数据源,采用纯Java Servlet,目的是为了在任何Java Web项目当中都可以使用,前端日历框架使用了FullCalendar与BootStrpa模态效果。

首先粘贴Servlet代码,从XML中获取数据,代码如下:

package com.yu.wday;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;

import com.yu.wday.util.Dom4JUtil;
import com.yu.wday.util.ResponseSendJson;

/**
 * qq80303857
 * Servlet implementation class FullCanlendarServlet
 */
@WebServlet("/FullCanlendarServlet")
public class FullCanlendarServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FullCanlendarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @SuppressWarnings({ "unused", "unchecked" })
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String path = request.getServletContext().getRealPath("/");
        File file = new File(path+"FullCanlendar/note.xml");
        Document document = Dom4JUtil.parse(file);
        Element root = document.getRootElement();

        List<Element> list = root.elements("event");

        String json = Dom4JUtil.XML2Json(list);

        ResponseSendJson.sendJson(response, json);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

点击日历增加休息日标签与点击休息日标签删除标签分别使用了GET与POST区分,Servlet代码如下:

package com.yu.wday;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.Element;

import com.yu.wday.util.Dom4JUtil;
import com.yu.wday.util.ResponseSendJson;

/**
 * qq80303857
 * Servlet implementation class FullCanlendarSaveTag
 */
@WebServlet("/FullCanlendarSaveTag")
public class FullCanlendarSaveTag extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FullCanlendarSaveTag() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    @SuppressWarnings("unused")
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String path = request.getServletContext().getRealPath("/");
        String start = request.getParameter("start");
        String title = request.getParameter("title");
        if (title!=null){
            title = new String(title.getBytes("ISO-8859-1"),"UTF-8");
        }else{
            title = "休息日";
        }
        File file = new File(path+"FullCanlendar/note.xml");
        Document document = Dom4JUtil.parse(file);
        Element root = document.getRootElement();
        Element element = root.elementByID("S"+start);
        if (element!=null){
            ResponseSendJson.sendJson(response, "{\"success\":\"exist\"}");
        }else{
            Element curEle = root.addElement("event");
            curEle.addAttribute("ID", "S"+start);
            curEle.addAttribute("start", start);
            curEle.addAttribute("title", title);
            Dom4JUtil.write(file, document);
            ResponseSendJson.sendJson(response, "{\"success\":\"sav\"}");
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String path = request.getServletContext().getRealPath("/");
        String id = request.getParameter("id");

        File file = new File(path+"FullCanlendar/note.xml");
        Document document = Dom4JUtil.parse(file);
        Element root = document.getRootElement();
        Element element = root.elementByID(id);
        if (element!=null){
            root.remove(element);
            Dom4JUtil.write(file, document);
            ResponseSendJson.sendJson(response, "{\"success\":\"del\"}");
        }else{
            root.remove(element);
            Dom4JUtil.write(file, document);
            ResponseSendJson.sendJson(response, "{\"success\":\"exist\"}");
        }
    }

}

初始化休息日与清空所有标签,分别使用GET与POST区分,Servlet代码如下:

package com.yu.wday;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.Element;

import com.yu.wday.util.DateUtils;
import com.yu.wday.util.Dom4JUtil;
import com.yu.wday.util.EveryDayUtils;
import com.yu.wday.util.ResponseSendJson;

/**
 * Servlet implementation class FullCanlendarInit
 */
@WebServlet("/FullCanlendarInit")
public class FullCanlendarInit extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public FullCanlendarInit() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String path = request.getServletContext().getRealPath("/");
        File file = new File(path+"FullCanlendar/note.xml");
        Document document = Dom4JUtil.parse(file);
        Element root = document.getRootElement();
        Dom4JUtil.clear(root, root.elements("event"));
        Dom4JUtil.write(file, document);
        ResponseSendJson.sendJson(response, "{\"success\":\"clear\"}");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String path = request.getServletContext().getRealPath("/");
        File file = new File(path+"FullCanlendar/note.xml");
        Document document = Dom4JUtil.parse(file);
        Element root = document.getRootElement();
        Dom4JUtil.clear(root, root.elements("event"));
        String parmYear = request.getParameter("year");
        Calendar c = Calendar.getInstance();
        int year = 2016;
        if (EveryDayUtils.strIsNum(parmYear)){
            year = Integer.valueOf(parmYear);
        }else{
            year = c.get(Calendar.YEAR);
        }

        for (int i=0;i<12;i++){
            c.set(year, i, 1);
            int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
            for (int x=1;x<=lastDay;x++){
                c.set(year, i, x);
                if(DateUtils.checkHoliday(c)){
                    System.out.print(year+"-"+(i+1)+"-"+x+" 休息    ");
                    String mon = "";
                    String day = "";
                    if (i+1<10){
                        mon = "0"+(i+1);
                    }else{
                        mon = ""+(i+1);
                    }
                    if (x<10){
                        day = "0"+x;
                    }else{
                        day = ""+x;
                    }
                    String ymd = year+"-"+mon+"-"+day;
                    Element curEle = root.addElement("event");
                    curEle.addAttribute("ID", "S"+ymd);
                    curEle.addAttribute("start", ymd);
                    curEle.addAttribute("title", "休息日");
                }else{
                    System.out.print(year+"-"+(i+1)+"-"+x+"    ");
                }
            }
            System.out.println();
        }
        Dom4JUtil.write(file, document);
        ResponseSendJson.sendJson(response, "{\"success\":\"init\"}");
    }

}

Dom4J工具包,代码如下:

package com.yu.wday.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4JUtil {

    public static Document parse(File file){
        SAXReader reader = new SAXReader();
        Document document = null;
        try {
            document = reader.read(file);
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return document;
    }

    public static void bar(Document document) throws DocumentException {

        Element root = document.getRootElement();

        // iterate through child elements of root
        for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
            Element element = (Element) i.next();
            // do something
        }

        // iterate through child elements of root with element name "foo"
        for ( Iterator i = root.elementIterator( "foo" ); i.hasNext(); ) {
            Element foo = (Element) i.next();
            // do something
        }

        // iterate through attributes of root
        for ( Iterator i = root.attributeIterator(); i.hasNext(); ) {
            Attribute attribute = (Attribute) i.next();
            // do something
        }
     }

    public Document createDocument() {
        Document document = DocumentHelper.createDocument();
        Element root = document.addElement( "root" );

        Element author1 = root.addElement( "author" )
            .addAttribute( "name", "James" )
            .addAttribute( "location", "UK" )
            .addText( "James Strachan" );

        Element author2 = root.addElement( "author" )
            .addAttribute( "name", "Bob" )
            .addAttribute( "location", "US" )
            .addText( "Bob McWhirter" );

        return document;
    }

    public static String XML2Json(List<Element> list){
        StringBuffer sb = new StringBuffer("[");
        for (int i=0;i<list.size();i++){
            StringBuffer sbTemp = new StringBuffer("{");
            Element e = list.get(i);
            Attribute abID = e.attribute("ID");
            Attribute abTitle = e.attribute("title");
            Attribute abStart = e.attribute("start");
            String idValue = abID.getValue();
            String title = abTitle.getValue();
            String start = abStart.getValue();
            sbTemp.append("\"start\":\""+start+"\",");
            sbTemp.append("\"title\":\""+title+"\",");
            sbTemp.append("\"id\":\""+idValue+"\"");
            if (i<list.size()-1){
                sbTemp.append("},");
            }else{
                sbTemp.append("}");
            }
            sb.append(sbTemp);
        }
        sb.append("]");
        return sb.toString();
    }

    public static void write(File file,Document document) throws IOException {

        FileOutputStream fos = new FileOutputStream(file);
        OutputFormat of = OutputFormat.createPrettyPrint();
        of.setEncoding("UTF-8");
//        of.setIndent("    ");
        XMLWriter writer = new XMLWriter(fos,of);

        writer.write(document);
        writer.flush();
        writer.close();
    }

    public static void clear (Element parentElement,List<Element> list){
        for (Element ele:list){
            parentElement.remove(ele);
        }
    }
}

检查休息日,代码如下:

package com.yu.wday.util;

import java.util.Calendar;

public class DateUtils {

    /**
      *
      * <p>Title: checkHoliday </P>
      * <p>Description: TODO 验证日期是否是节假日</P>
      * @param calendar  传入需要验证的日期
      * @return
      * return boolean    返回类型  返回true是节假日,返回false不是节假日
      * throws
      * date 2014-11-24 上午10:13:07
      */
     public static boolean checkHoliday(Calendar calendar){  

         //判断日期是否是周六周日
         if(calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY ||
                 calendar.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY){
             return true;
         }
         return false;
     }
}

常用工具栏,判断字符串是否可以转换为数字,代码如下:

package com.yu.wday.util;

public class EveryDayUtils {

    public static boolean strIsNum(String str){
        try {
            Integer.valueOf(str);//把字符串强制转换为数字
            return true;//如果是数字,返回True
        } catch (Exception e) {
            return false;//如果抛出异常,返回False
        }
    }

}

使用Response发送Json数据,代码如下:

package com.yu.wday.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

public class ResponseSendJson {

    public static void sendJson(HttpServletResponse response,String json){

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");  

        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.append(json);
        out.flush();
        out.close();
    }
}

有了这些数据,就很轻而易举的判断哪天是休息日咯。

完整项目下载地址:http://aa80303857.iteye.com/admin/blogs/2346586

时间: 2024-11-09 00:21:52

Java 设定工作日日历的相关文章

Java SSH 项目总结——设定工作日日历

前言 今天文章的主题就是日历,先介绍下背景,这个日历是为一个在线预约系统服务的,在线预约就是选择工作日,然后进行具体业务的预约,而工作日的是设定就是通过我们这个日历来完成的. 正题 效果 我们先来看下日历的效果图,第一张是当前月的日历,第二张是设定完工作日的日历, 分解 我们要实现这个工作日设定的日历的功能,将这个功能进行分解: 1.实现一个万年历. 2.通过界面点击日历将工作日保存到数据库中 3.将数据库中已经设定好的日期渲染到界面日历中以不同的颜色进行标识. 实现 数据库设计 数据库中需要保

Effective Java设定游戏 - 就是爱Java

首先,我们先设定游戏,一个网页游戏的基本场景,主角拥有各种能力,但一开始数值都只有系统初始,随着故事的发展,会接触到各种不同的场景,获得提升角色的道具与装备,来参与更高难度的任务. 阅读全文>> Effective Java设定游戏 - 就是爱Java,布布扣,bubuko.com

JAVA学习笔记-日历程序

package myCalendar; import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.Scanner; /** * 可视化日历 * @author iwang

Java之可视化日历小练习

20150802 看完时间日期类Date和DateFormat以及日历类Calendar和GregorianCalendar类之后做个小日历来练习一下相应的方法和属性,加强一下记忆: 描述:输入指定格式的日期如:2001-12-25,输出当月的日历 思路: 1.先从键盘输入指定格式的字符串(str) 2.将字符串用格式转换类转换成日期( Date date = format.parse(str) ) 3.将日期设置成日历类( calendar.setTime(date) ) 4.获取当月第一天是

java版的日历程序

//MainFrame.java package com.huowolf.myCalender; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calen

Java中的日历类/集合类/数学类/正则表达式/数组工具类等的常用方法与基本功能

一. Arrays 针对数组操作的工具类,提供了一些针对数组排序和二分搜索的方法.常用方法:1.public static String toString(int[] a):将int类型的数组转换成字符串.转换成字符串后输出形式: ([元素1,元素2,元素3...]),Arrays在底层重写了toString方法.2.public static void sort(int[] a):对指定的 int 型数组按数字升序进行排序.3.public static int binarySearch(in

java GUI简单日历查看

import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Calendar; public class swingCalendar extends JFrame implements ActionListener { public static swingCalendar u1; private

Java实现打印日历的功能

编写一个程序,显示给定年月的日历.程序提示用户输入年份和月份,然后显示该月的整个日历. 代码: 1 import java.util.Scanner; 2 public class PrintCalendar{ 3 public static void main(String[] args){ 4 Scanner input=new Scanner(System.in); 5 6 System.out.print("Enter full year(e.g.,2011): "); 7 i

Java+Selenium——如何处理日历控件——方法二

有些web产品,网页上有一些时间选择,然后支持按照不同时间段范围去筛选数据.网页上日历控件一般,是一个文本输入框,鼠标点击, 就会弹出日历界面,可以选择具体日期.这篇,就是介绍用selenium如何自动化脚本实现. 先看一个完全模仿手工操作的场景,点击输入框,点击下一个月,再点击一个日子. package rjcs; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar;