Java随机数和日期相减

<%@ include file="../../inc/head.jsp"%>
<%@ page language="java"
	import="java.sql.*,java.util.*,com.zj.comm.util.Common,com.zj.hz.bizq.WapSpecialNews,com.zj.hz.vo.*,com.zj.hz.util.*,java.text.*"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<wml>
<head>
	<meta http-equiv="Cache-Control" content="max-age=0" />
	<meta http-equiv="Cache-Control" content="no-cache" />
</head>
<card id="index1" title="<%=Common.getPropertiesValue("SITETITLE")%>">
<p>
<a href="./">首页</a>>>抢股<br/>
<%
		//	得到当前用户和用户的登入时间
		String mobile = Common.getMobile(request);
//		java.util.Date now1= new java.util.Date();
//		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//		String nowtime = f.format(now);
		Calendar cal=Calendar.getInstance();
		SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String nowtime=sf.format(cal.getTime());
//		java.util.Date nowtime=f.parse(nowtime1);
		//得到随机数
		Random rand = new Random();
		int ran = rand.nextInt(24);
		//数据库连接
		Connection conn = null;
		PreparedStatement ps = null;
		PreparedStatement pst = null;
		PreparedStatement psi = null;
		PreparedStatement psl = null;
		PreparedStatement psm = null;
		PreparedStatement psn = null;
		PreparedStatement psq = null;
		ResultSet rsm = null;
		ResultSet rst = null;
		ResultSet rsn = null;
		ResultSet rsq = null;
		try {
			conn = DBClassLocal.getConnection();
			String sqli = "select count(mobile) from hd_hotbuy where mobile='"+ mobile + "'";
			pst = conn.prepareStatement(sqli);
			rst = pst.executeQuery();
			if (rst.next()) {
				int num = rst.getInt(1);
				if (num == 0) { 
			//		LocalJDBCTemplate.insertData("insert into hd_spring (username,userphone,sign,mobile,person) values (?,?,?,?,?)",new Object[]{name,phone,"lake",mobile,person});
					LocalJDBCTemplate.insertData("insert into hd_hotbuy (usertime,mobile) values (?,?)",new Object[]{nowtime,mobile});
					try{
					String sqq="select * from  hd_hotbuy  where random='"+ran+"'";
					psq=conn.prepareStatement(sqq);
					rsq=psq.executeQuery();
				//	out.println(rsq);
					String aa="";
					String stock="";
					String point="";
					if(rsq.next()){
						 stock = rsq.getString("rank");
						 point = rsq.getString("point");
								aa="恭喜您成功抢到【" + stock + "】,得到【"+ point + "】个积分!";
								out.println("恭喜您成功抢到【" + stock + "】,得到【"+ point + "】个积分!");
								LocalJDBCTemplate.insertData("insert into hd_hotbuy_log (content,mobile) values(?,?)",new Object[]{aa,mobile});
							//	out.print("ssssssssss");
					}
					}catch(Exception e){
						e.printStackTrace();
					}finally{
							if(psq!=null){
									psq.close();
								}
								if(rsq!=null){
									rsq.close();
								}
					}
				} else {
					String sqll = "select * from (select * from hd_hotbuy order by createtime desc ) where rownum<=1 and mobile='"+mobile+"'";
					psm = conn.prepareStatement(sqll);
					rsm = psm.executeQuery();
					while (rsm != null && rsm.next()) {
					//	SimpleDateFormat sf=new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
						String as1=sf.format(rsm.getTimestamp("createtime").getTime());
						java.util.Date as=sf.parse(as1);
				//		out.println(nowtime);
				//		out.println("<br/>"+"下面是上一次的时间"+"<br/>");
				//		out.println(as);
						java.util.Date nowtime1=sf.parse(nowtime);
						long result = (nowtime1.getTime() - as.getTime())/3600000;
					//	out.println(result);
						if (result<1) {
							out.println("对不起,本时段已经抢过了,过一个小时再来吧!");
						} else {
							try{
							LocalJDBCTemplate.insertData("insert into hd_hotbuy (usertime,mobile)values (?,?)",new Object[]{nowtime,mobile});
							String sqlm = "select * from hd_hotbuy where random='"+ ran + "'";
							psn = conn.prepareStatement(sqlm);
							rsn = psn.executeQuery();
							String bb="";
							String stock="";
							String point="";
							if (rsn.next()) {
								stock = rsn.getString("rank");
								point = rsn.getString("point");
								out.println("恭喜您成功抢到【" + stock + "】,得到【"
										+ point + "】个积分!");
								bb="恭喜您成功抢到【" + stock + "】,得到【"+ point + "】个积分!";		
								LocalJDBCTemplate.insertData("insert into hd_hotbuy_log (content,mobile) values(?,?)",new Object[]{bb,mobile});
							//	out.print("gggggg");
							}
							}catch(Exception e){
								e.printStackTrace();
							}finally{
								if(psn!=null){
									psn.close();
								}
								if(rsn!=null){
									rsn.close();
								}
							}
					}
					}

				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(conn!=null){
				conn.close();
			}
			if(rst!=null){
				rst.close();
			}
			if(pst!=null){
				rst.close();
			}
			if(conn!=null){
				rst.close();
			}
			
		}
	%> 
<br/>
<%@ include file="../../inc/bottom.jsp" %>
</p>
</card>
</wml>

你可能感兴趣的:(java,spring,sql,jsp,mobile)