时间知识点
SimpleDateFormat 显示时间形式 simpleDateFormat.format(new Date());
取得毫秒数 new Date().getTime();
String(时间形式的) 转 date
Date date=format.parse(Strdate);
表结构 testcount
id ip seetime num
int nvchar nvchar bigint
原程序count24ip.jsp
<%@ page language="java" contentType="text/html; charset=GB2312" import="java.sql.*,java.text.*,java.util.Date"%>
<jsp:useBean id="db" class="com.pp.db.DBOperation"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'count24ip.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
db.creatConnection();
String ip=request.getRemoteAddr();
Date date=new Date();
boolean same=false;
String tempip="";
String seetime="";
Boolean flag=false;//没有超过一天了。
Date date=new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String nowdate=format.format(date);//写入数据库的当前 2008-03-26 13:38:44
//当前的系统时间转成以毫秒为单位的时间
long longdate=date.getTime(); //返回指定 Date 对象自 1970 年 1 月 1 日午夜(通用时间)以来的毫秒数。
String sql="select * from testcount";
String sql1="insert into testcount(ip,seetime,num) values('"+ip+"','"+nowdate+"',1)";
String sql3="update testcount set num=num+1,seetime='"+nowdate+"' where ip='"+ip+"'";
ResultSet rs= db.executeQuery(sql);
if(!rs.next())
{ //没有数据就直接插入这条数据
if(db.executeUpdate(sql1)){
out.print("计数成功");
}
}
else
{ rs.beforeFirst();
while(rs.next())
{
String dbip= rs.getString("ip");
if(dbip.equals(ip))
{same=true; //same判断有没有相同ip,true为有
tempip=dbip;
seetime=rs.getString("seetime");
}
}
if(same)
{
//该ip已经访问过
Date time2= format.parse(seetime); //取数据库取时间 String取Date类型
Long seetimel=time2.getTime();
long aaday=seetimel+24*60*60*1000; //上次登录时间24小时后的毫秒数
if(longdate>aaday)
{
out.println("距离上次登录时间超过24小时");
flag=true;
}
if(flag) //超过一天
{
//可以记录这次计数
if(db.executeUpdate(sql3)){
out.print("更新计数成功");
}
}
else
{
//一天内同一个ip记无效
out.print("一天内同一个ip记无效");
}
}
else
{
if(db.executeUpdate(sql1)){
out.print("计数成功");
}
}
}
db.closeConnection();
%>
</body>
</html>