利用JSP实现日历和当前天数显示,附加CSS特效

2 篇文章 0 订阅

实验目标:

实验
提示
用Calendar日历对象得到当前的年月日
为了方便在表格中显示,应该得到本月的1号是一个星期的第几天(周日被认为是第一天),得到1号是一个星期的第几天需要你先设置日历对象的“日”为1号


学习内容:

JSP基本原理,输入输出语句,HTML语言


学习时间:

  • 实验课

代码产出:

  • 技术笔记 2 遍
  • CSDN 技术博客 3 篇
  • <%@ page contentType="text/html; charset=gb2312"%>
    <%@ page import="java.util.Calendar" %>
    
    <!DOCTYPE html>
    <html>
    <head>
    <%--    <meta charset="UTF-8">--%>
        <meta http-equiv="Content-Type" content="text/html charset=gb2312">
        <title>日历</title>
    </head>
    <body>
    <h1>Baizhimin</h1>
    <table border="1">
        <tr style="background-color: yellow">
            <th>日</th>
            <th>一</th>
            <th>二</th>
            <th>三</th>
            <th>四</th>
            <th>五</th>
            <th>六 </th>
        </tr>
    
        <%
            // 获取当前日期
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            int month = cal.get(Calendar.MONTH);
            int dayOfMonth = cal.get(Calendar.DAY_OF_MONTH);
            //第一行显示日期
    out.print(year);out.print("年");out.print(month+1);out.print("月");
            // 将日历对象的“日”设置为1号
            cal.set(Calendar.DAY_OF_MONTH, 1);
    
            // 获取本月1号是星期几
            int firstDayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
    
            // 获取本月共有多少天
            int lastDayOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    
            // 输出表格
            int day = 1;
            for (int i = 0; i < 6; i++) {
                out.print("<tr>");
                for (int j = 1; j <= 7; j++) {
                    if (i == 0 && j < firstDayOfWeek) {
                        out.print("<td bgcolor=\"gray\"></td>");
                    } else if (day > lastDayOfMonth) {
                        out.print("<td bgcolor=\"gray\"></td>");
                    } else {
                        if (day==dayOfMonth){
                            out.print("<td bgcolor=\"red\">" + day + "</td>");
    
                            }
                      else   out.print("<td bgcolor=\"gray\">" + day + "</td>");
                        day++;
    
                    }
    
                }
                out.print("</tr>");
                if (day > lastDayOfMonth) {
                    break;
                }
    
            }
    
        %>
    
    </table>
    </body>
    </html>

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值