1:先设计需要展示的页面,
2:日历中得有法定节假日
3:日历需要可以手动设置假日
4:可以自动修改年份
网上找到日历控件;(直接去jquery插件库中找需要的控件就可以了,可以直接查看,看到的基本上就是需要的样子,加入到项目中再微调一下样式就可以了。一般控件会直接有调用的方法,有控件需要导入的js,css,会告诉你需要哪几个文件,直接下载就行)
控件需要的准备工作:
导入需要的js,css,然后页面打开的时候调用就可以。调用成功之后,给控件中传入需要的参数即可
$(’#div1’).fullYearPicker({ //div1就是日历控件需要放的位置
disable:false,//只读
year:year,//指定年份
initDate:data,//初始化选中日期
yearScale: { min: 1949, max: 2100 },//初始化日历范围
format:“YYYY-MM-DD”,//日期格式化 YYYY-MM-DD YYYY-M-D
cellClick: function (dateStr, isDisabled) {//当前选中日期回调函数
},
choose:function (a) {//实时获取所有选中的日期的回调函数(推荐使用)
$("#dateJson").val(JSON.stringify(a));
}
});
2:通过后台拿到所有的法定假日(只是拿到预设的,期间法定假日有修改,客户自己手动设置)
检查一个日期是否为节假日 http://www.easybots.cn/api/holiday.php?d=20130101
检查一个日期是否为节假日 http://www.easybots.cn/api/holiday.php?d=20130101
检查多个日期是否为节假日 http://www.easybots.cn/api/holiday.php?d=20130101,20130103,20130105,20130201
获取2012年1月份节假日 http://www.easybots.cn/api/holiday.php?m=201201
获取2013年1/2月份节假日 http://www.easybots.cn/api/holiday.php?m=201301,201302
get请求,d后边为参数
3:页面上展示可修改年份
把通过接口得到的参数传到前端显示
要灵活的设置年份的话,就需要每次点击年份,去调取当年的数据,如果数据库中没有存当前的日期,就需要通过接口调取
后台代码:
@Controller
@RequestMapping({"/aaa/*"})
public class WorkCalController {
/*
* 根据后台传的id和年份,先查找数据库,数据库中存在当年数据的话就直接显示,没有的话就调用接口
*/
@RequestMapping("getHoliday")
public void getHoliday(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap)throws WafException, WafBizException, EASBizException, BOSException{
String id = request.getParameter("id");
String year = request.getParameter("year");
WorkCalenderInfo info = null;
String dateJson = "";
if(StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(year)){
SelectorItemCollection sic=new SelectorItemCollection();
sic.add("id");
sic.add("year");
sic.add("dateJson");
WorkCalenderCollection col=WorkCalenderFactory.getRemoteInstance().getWorkCalenderCollection("select * where year=" + year);
if(col != null && col.size() > 0){
info = col.get(0);
if(info != null){
dateJson = info.getDateJson();
}
}else{
String reqDate = year+"01,"+year+"02,"+year+"03,"+year+"04,"+year+"05,"+year+"06,"+year+"07,"+year+"08,"+year+"09,"+year+"10,"+year+"11,"+year+"12";
List dateList = requestDate(reqDate);
dateJson = JSON.toJSONString(dateList);
}
}
JSONUtils.SUCCESS(dateJson);
}
/*
* 访问法定节假日的url,获取传入日期是否为节假日
*/
private List requestDate(String httpArg){
String jsonResult = null;
Map map = new HashMap();
List list = new ArrayList();
try {
String httpUrl = "http://www.easybots.cn/api/holiday.php"; //接口的url
BufferedReader reader = null;
StringBuffer sbf = new StringBuffer();
httpUrl = httpUrl + "?m=" + httpArg; //接口需要的参书,法定节日的这个接口需要传当前的年月
URL url = new URL(httpUrl); //创建一个url的链接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();//表示到 URL 所引用的远程对象的连接,打开链接
connection.setRequestMethod("GET");//连接对象设置方式
connection.connect();//进行链接
InputStream is = connection.getInputStream();//
reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String strRead = null;
while((strRead = reader.readLine()) != null){
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
jsonResult = sbf.toString();
JSONObject json = new JSONObject(jsonResult);
Iterator iterator = json.keys();
while(iterator.hasNext()){
String key = (String) iterator.next();
Object value = json.get(key);
StringBuilder sb = new StringBuilder(key);//构造一个StringBuilder对象
sb.insert(4, "-");//在指定的位置1,插入指定的字符串
key = sb.toString();
Map maps = (Map)JSON.parse(value.toString());
Set<Map.Entry<String, String>> entries = maps.entrySet();
for (Map.Entry<String, String> entry : entries) {
String mapKey = entry.getKey() ;
String mapValue =entry.getValue();
if("1".equals(mapValue) || "2".equals(mapValue)){
String dateJson = key + "-" + mapKey;
list.add(dateJson);
}
}
map.put(key, value);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}