首页 技术 正文
技术 2022年11月20日
0 收藏 399 点赞 4,152 浏览 2257 个字

曾经初学java写万年历,都是採用主要的算法求出是否闰年闰月 计算公式例如以下

int year = 2014, month = 8, total = 0;if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
|| month == 10 || month == 12) {
total = 31;
} else if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
total = 29;
} else {
total = 28;
}
} else {
total = 30;
}

total 即为当月天数

忽然认为,事实上java 有个Calendar类 这个就能够获取当月有多少天,实现日历的核心就是获取当月有多少天。

因项目须要仅仅需列出当月日历就可以,无需其它

编写代码 生成html

package Str;import java.util.Calendar;
/**
*
*
* @project Tool
* @type Test8
* @Description
* @author xuyw
* @email xyw10000@163.com
* @date 2014-8-9 下午01:45:02
* @version 1.0
*
*/
public class Test8 {/**
* @param args
*/
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, 0);
calendar.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
int firstDay = calendar.get(Calendar.DAY_OF_WEEK) - 1;
int monthDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);// 当月最后一天StringBuilder sbd = new StringBuilder();
sbd.append("<table cellspacing='0' style='background: url(./img/body.png);'>");
sbd.append("<thead><tr>");
sbd
.append("<th>星期天</th><th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th><th>星期五</th><th>星期六</th>");
sbd.append("</tr></thead>");
sbd.append("<tbody><tr>");
// 第一行
int weekend = 0;// 每周的最后一天的日期
for (int i = 0; i < 7; i++) {
if (i < firstDay) {
sbd.append("<td></td>");
} else {
sbd.append("<td>" + (i - firstDay + 1) + "</td>");
}
weekend = i - firstDay + 1;
}
sbd.append("</tr>");
while (weekend < monthDays) {
sbd.append("<tr>");for (int i = 0; i < 7; i++) {
if (weekend + i < monthDays)// 数字小于等于当前月的最后一天
{
sbd.append("<td>" + (i + weekend + 1) + "</td>");
} else {
sbd.append("<td></td>");
}}
sbd.append("</tr>");
weekend += 7;// 周末再加7天
}sbd.append("<tbody></table>");
System.out.println(sbd.toString());
}
}

先前測试就用控制台生成html标签   将生成的html标签在页面展示就可以 效果图例如以下

css

* {
/* old-style reset here :) */
border: 0px;
padding: 0px;
}
body {
font-family: Helvetica;
background: white;
text-align: center;
/* background: url(../img/body.png) repeat-x; */}
body h1 {
padding-top: 20px;
font-size: 36px;
color: #335;
}
p{font-size:26px;}
table {
border-collapse: separate;
border: 1px solid #9DABCE;
border-width: 1px 1px 1px 1px;
margin: 10px auto;
font-size: 30px;
}td, th {
width: 81px;
height: 81px;
text-align: center;
vertical-align: middle;
/*background: url(../img/cells.png);*/
color: #444;
position: relative;
}
th {
height: 30px;
font-weight: bold;
font-size: 14px;
}td.sign_in {
background-position: 81px 0px;
color: red;
}td.sign_no{
background: url(../img/cells.png);
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,958
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,482
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,328
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,111
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,743
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,777