用JAVA写一个日历计划
效果图(自己需要在前台加css修饰)
代码部门
public class monthUtils
{
static int[] _days = new int[]
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private Date td;
/**
* 构造函数,必填
*
* @param year
* @param month
*/
public monthUtils(String year, String month)
{
this.td = this.initMonth(year, month);
}
/**
* 根据年份和月初始化
*
* @param year
* @param month
* @return
*/
Date initMonth(String year, String month)
{
return DateKit.str2Date(year + "-" + month + "-01");
}
/**
* 获取月份的日期数
*
* @return
*/
public int getDAYS()
{
Calendar cal = Calendar.getInstance();
cal.setTime(td);
int days = _days[cal.get(Calendar.MONTH)];
int year = cal.get(Calendar.YEAR);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
days = 29;
}
return days;
}
/**
* 获取星期几,第一天为星期日
*
* @return
*/
int getFirst()
{
Calendar cal = Calendar.getInstance();
cal.setTime(td);
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* 获取日期的数组集
*
* @return
*/
public String[] getDAYOfARR()
{
String[] arr = new String[42];
int days = getDAYS();
int firstDate = getFirst();
for (int n = 0; n < 42; n++)
{
int d = n - firstDate + 2;
if (n > firstDate - 2 && d <= days)
{
arr[n] = String.valueOf(d);
}
else
{
arr[n] = "";
}
}
return arr;
}
/**
* 获取yyyy-mm-dd日期格式数组
*
* @return
*/
public String[] getYMD()
{
Calendar cal = Calendar.getInstance();
cal.setTime(td);
String[] arr = new String[42];
int days = getDAYS();
int firstDate = getFirst();
for (int n = 0; n < 42; n++)
{
int d = n - firstDate + 2;
if (n > firstDate - 2 && d <= days)
{
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), d);
arr[n] = DateKit.date2Str(cal.getTime());
}
else
{
arr[n] = "";
}
}
return arr;
}
/**
* 生成表格()
*
* @param css
* @return
*/
public String getTable(String css)
{
StringBuffer sb = new StringBuffer();
sb.append("<table class=\"" + css + "\">");
sb.append("<tr>");
sb.append("<td>");
sb.append("星期日");
sb.append("</td>");
sb.append("<td>");
sb.append("星期一");
sb.append("</td>");
sb.append("<td>");
sb.append("星期二");
sb.append("</td>");
sb.append("<td>");
sb.append("星期三");
sb.append("</td>");
sb.append("<td>");
sb.append("星期四");
sb.append("</td>");
sb.append("<td>");
sb.append("星期五");
sb.append("</td>");
sb.append("<td>");
sb.append("星期六");
sb.append("</td>");
sb.append("</tr>");
String[] data = this.getYMD();
sb.append("<tr>");
int n = 1;
for (String str : data)
{
if (StringUtils.isEmpty(str) && n == 36)
{
break;
}
sb.append("<td id=\"d" + str + "\" style=\"font-size:4px;\">");
sb.append(str);//每天想要显示的信息就在添加
sb.append("</td>");
if (n % 7 == 0)
{
sb.append("</tr><tr>");
}
n++;
}
sb.append("</tr>");
sb.append("</table>");
return sb.toString();
}
}
// Controller(这里用的是jfinal框架)后台调用后返回到页面就形成一个日历表格
public void getTable(){
Member _member = getSessionAttr(consts.tmcSessionKey);
//包不同的月份从前台传过来就好了
String year=getPara("year");
String month=getPara("month");
monthUtils monthUtils=new monthUtils(year,month);
String table=monthUtils.getTable(list);
renderText(table);
}
推荐一款很好看,也很实用的js日历控件
http://sentsin.com/layui/laydate/