java js 日期

<%@ page language="java" pageEncoding="UTF-8"
    import="java.util.*,java.text.SimpleDateFormat,com.common.*"%>

<%
//ajax must use!!!   url path
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
//System.out.println(basePath);
%>



<%
        //判断是切换用户还是当前用户点击菜单加载本页面
        String type=request.getParameter("type")==null?"":request.getParameter("type").toString();
        String username="";
        String userid="";
        String curdate="";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        GregorianCalendar cal = new GregorianCalendar();
        curdate=sdf.format(new Date());
        if(type.equals(""))  //初次请求页面,默认值
        {
            username =request.getSession().getAttribute("loginUser")==null?"":request.getSession().getAttribute("loginUser").toString();
            userid=request.getSession().getAttribute("loginID")==null?"":request.getSession().getAttribute("loginID").toString();
            curdate=sdf.format(new Date());        
        }
        if(type.equals("disp_premonth"))
        {
            userid=request.getParameter("userid")==null?"":request.getParameter("userid").toString();
            String c_username=request.getParameter("username")==null?"":request.getParameter("username").toString();
            username=new String(c_username.getBytes("ISO-8859-1"),"UTF-8");
            //System.out.println("username=" + username + "," + userid);
            //根据请求日期计算上月
            curdate=request.getParameter("curdate")==null?"":request.getParameter("curdate").toString();
            if(curdate.equals(""))
              curdate=sdf.format(new Date());
            Date t_curdate=new Date();
            try{t_curdate=sdf.parse(curdate);}catch(Exception e){}
            cal.setTime(t_curdate); cal.add(Calendar.MONTH, -1);
            curdate=sdf.format(cal.getTime());
            //System.out.println("curdate=" + curdate);
        }
        if(type.equals("disp_nextmonth"))
        {
            userid=request.getParameter("userid")==null?"":request.getParameter("userid").toString();
            String c_username=request.getParameter("username")==null?"":request.getParameter("username").toString();
            username=new String(c_username.getBytes("ISO-8859-1"),"UTF-8");
            //System.out.println("username=" + username + "," + userid);
            //根据请求日期计算上月
            curdate=request.getParameter("curdate")==null?"":request.getParameter("curdate").toString();
            if(curdate.equals(""))
              curdate=sdf.format(new Date());
            Date t_curdate=new Date();
            try{t_curdate=sdf.parse(curdate);}catch(Exception e){}
            cal.setTime(t_curdate); cal.add(Calendar.MONTH, 1);   //!!
            curdate=sdf.format(cal.getTime());
            //System.out.println("curdate=" + curdate);
        }        
%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <Meta http-equiv="Pragma" Content="No-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <title>个人日志</title>
        <link href="CSS/style.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <div class="pageContent">
    <form method="post" action="dwz-ria-1.4.6/demo/common/ajaxDone.html" class="pageForm required-validate" onsubmit="return validateCallback(this, navTabAjaxDone);">
        <div class="pageFormContent" layoutH="56">
        
        <table width="100%" border="0" cellspacing="0" cellpadding="0"
            bgcolor="#D3EEFF">
            <tr>
                <td valign='center' style='font-size:13px;'>
                      &nbsp;&nbsp;&nbsp;&nbsp;          
                      当前是&nbsp;<font class="fonthong1"><%=username%></font>&nbsp;的日志
                      <span style='width:40px;'></span>
                      <input type="text" name="date6" class="date"  readonly="true" />
                      
                </td>
            </tr>

            <!-- ------2222222222222222222222-->
            <tr>
                <td align="center">
                    <table width="100%" border="0" cellpadding="0" cellspacing="1"
                        bgcolor="#8cd5f5">
                        <tr bgcolor="#8cd5f5">
                            <td width="40px" height="30" align="center">&nbsp;</td>
                            <td width="13%" height="30" align="center">星期日    </td>
                            <td width="13%" align="center">星期一    </td>
                            <td width="13%" align="center">星期二</td>
                            <td width="15%" align="center">星期三    </td>
                            <td width="15%" align="center">星期四</td>
                            <td width="13%" align="center">星期五    </td>
                            <td width="13%" align="center">星期六</td>
                        </tr>
    <%
        //根据传入日期,生成本月及跨上、下月日历单元格。
        //..设置日历对象时间为传入参数日期
        Date m_day=new Date();
        try{ m_day=sdf.parse(curdate); }catch(Exception e){}        
        cal.setTime(m_day);
        //..本月1号对应星期几(用来计算本月1号到上周日占上个月几天,周日 1,周一 2...)
        cal.set(Calendar.DATE,1);
        int week_firstdayofmonth=cal.get(Calendar.DAY_OF_WEEK);
        //System.out.println("本月1号:"+ sdf.format(cal.getTime()));
        //System.out.println("本月1号对应周几:"+ week_firstdayofmonth);
        //..本月最后1天对应星期几(用来计算本月底到周六需补下个月几天,周日 1,周一 2...)
        cal.set(Calendar.DATE,1);
        int days_ofmonth=cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //System.out.println("本月总共天数:"+ days_ofmonth);
        cal.add(Calendar.DAY_OF_MONTH, days_ofmonth-1);
        int week_lastdayofmonth=cal.get(Calendar.DAY_OF_WEEK);
        //System.out.println("本月最后1天:" + sdf.format(cal.getTime()));
        //System.out.println("本月最后一天对应周几:" + week_lastdayofmonth);
        //..获取日历显示的总天数 (1号前空的天数+本月天数+最后一天之后空的天数)
        int ibeforecells=(week_firstdayofmonth-1);  //1号之前的天数
        int ibehindcells=7-week_lastdayofmonth;     //月底之后的天数
        int cellcounts= ibeforecells+ days_ofmonth + ibehindcells;
        //System.out.println("日历单元格总数=" + cellcounts);
        //..获取日历所有单元格对应的日期并写入变量
        String days[];
        days=new String[cellcounts];
        //....单元格第1天日期(可能不是本月第一天  周日  周一  周二 周三 周四 周五 周六)
        cal.set(Calendar.DATE,1);
        cal.add(Calendar.DATE,-1*(week_firstdayofmonth-1));  //本月1好往前推到周日,即第1天。
        //System.out.println("单元格第一天:" + sdf.format(cal.getTime()));
        for(int i=0;i<cellcounts;i++)
        {
            days[i]=sdf.format(cal.getTime());
            cal.add(Calendar.DAY_OF_MONTH,1);
            //if(bdebug) System.out.println("填充变量:days" + i + "=" + days[i]);
        }
        //..查询数据,一次获取完毕
        String dt1=days[0] + " 00:00:00";
        String dt2=days[days.length-1] + " 23:59:59";
        Tdb db=new Tdb();
        String sql="select * from log where datetime>='" + dt1 + "' and datetime<='"
          + dt2 + "' and em_id='" + userid + "'";
        List rs=db.getrs(sql);
        //System.out.println(rs.size() + "," + sql);
        //..生成日志内容数据到data变量,与days变量一一对应。
        String data[];
        data=new String[days.length];
        for(int i=0;i<days.length;i++)  //逐一比较日历和查询结果,匹配后,写入数据变量
        {
          data[i]="";  //数据初始化
          //  
          for(int j=0;j<rs.size();j++)
          {
            String dt=((HashMap)rs.get(j)).get("datetime").toString();
            if(dt==null || dt.equals(""))
              ;
            else
            {
              Date dd=sdf.parse(dt);
              String db_dt=sdf.format(dd);
              if(days[i].toString().equals(db_dt))
              {
                String ct=((HashMap)rs.get(j)).get("content").toString();
                if(ct.length()>30) ct=ct.substring(0,30) + "...";  //超过20个字符,截取
                data[i]=ct;
                break;
              }
            }
          }
        }
        //..根据日历变量输出日历表格
        for (int i = 0; i < days.length; i++)
        {
            //if(bdebug) System.out.println("i=" + i);
            if(i%7==0)  //每7个输出一行标志
            {
                out.println("<tr bgcolor='#D3EEFF'>");
                //每行新增加一个功能,输出一个是本年的第几周
                Date everyrow_firstday=sdf.parse(days[i]);
                System.out.println("days[i]=" + days[i]);
                cal.setTime(everyrow_firstday);
                int iweek_ofyear=cal.get(Calendar.WEEK_OF_YEAR);
                out.println("<td height='80' valign='middle' align='center'>"
                    + "第" + iweek_ofyear + "周</td>");
            }
            //输出td
            //..计算单元格显示的当天序号,非本月不显示序号,颜色为灰色
            String tdhead="&nbsp;";
            String tdcolor="gray";
            if(i>=ibeforecells && i<(cellcounts-ibehindcells))  //当月内的天数
            {
               //if(bdebug) System.out.println(i);
               tdhead=String.valueOf(Integer.parseInt(days[i].substring(8,10)));  //截取日期后2位作序号
               tdcolor="#333333";  //#000000  字体不要太黑
            }            
            //..输出日历表格
            out.println("<td height='80' valign='top'>");
            out.println("  <div style='background-color: #D3EEFF; height: 50px; width: 97%; margin: 0px;cursor:hand;' "
                + " onclick='log_editdisp(\"" + userid + "\",\"" + days[i] + "\");'>");
            out.println("    <div width='99%' align='center'>");
            out.println("      <a href='#' onclick='log_editdisp(\"" + userid + "\",\"" + days[i] + "\");return false;'>" + tdhead + "</a>");
            out.println("    </div>");
            
            out.println("    <span style='margin: 0 0 0 0px;'>");
            out.println("      <a class='button' target='dialog' rel='dlg_page2' href='work_log_edit.jsp?id=2356'>");
            out.println("        <font color='" + tdcolor + "'>xxxx" + data[i] + "</font>");
            out.println("      </a>");
            out.println("    </span>");
            out.println("  </div>");
            out.println("</td>");
            //
            if(i%7==6)  //每7个输出一行结束标志
            {
              out.println("</tr>");
            }
          }
    %>
                    </table>
                </td>
            </tr>
        </table>

                
        </div>        
    </form>
</div>        
    </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值