response.setContentType("text/htmlcharset=UTF-8")
response.setHeader("Cache-Control", "no-cache")
PrintWriter out = response.getWriter()
try {
if ("submit".equals(request.getParameter("action"))) {
//交卷
//TODO:这里做交卷处理
out.println("已交卷")
} else {
//倒计时
//设置结束时间
Calendar timeend = Calendar.getInstance()
timeend.set(2008, 10, 7, 14, 50, 0)
long end = timeend.getTimeInMillis()
//out.print(sdf.format(timeend.getTime())+"<br>")
//获取当前时间
Calendar rightNow = Calendar.getInstance()
//out.print(sdf.format(rightNow.getTime())+"<br>")
long now = rightNow.getTimeInMillis()
//计算剩余时间
int left = (int) (end - now)
if (left <= 0) {
//时间到
out.print("over")
} else {
int leftHour = left / (1000 * 60 * 60)
left = left % (1000 * 60 * 60)
int leftMinute = left / (1000 * 60)
left = left % (1000 * 60)
int leftSecond = left / (1000)
out.print(leftHour + "时" + leftMinute + "分" + leftSecond + "秒")
}
}
} finally {
out.close()
}
以下是html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/htmlcharset=UTF-8">
<script src="../js/jquery.js" type="text/javascript"></script>
<script type="text/JavaScript">
$(document).ready(function(){
setInterval(getLeftTime,1000)
})
//得到剩余时间
function getLeftTime(){
$.get("/jquery/TimeServlet?",function(data){
if(data=="over"){
//window.location="../index.jsp"
$("#paperform").submit()
}
else{
$("#nowis").text(data)
}
})
}
</script>
</head>
<body>
<div id="nowis" ></div>
<form id="paperform" method="POST" action="/jquery/TimeServlet?action=submit">
</form>
</body>
</html>
其实你说的只是在servlet中处理时间,无非是拿到数据库中时间的字段,然后++--,然后set 然后就OK ,不懂继续问
用split() 方法切割不就好了~~~<script>
var str="2010-9-26 12:20:34"
time=str.split(/[-\s:/]/)
document.write("时间为:"+str + "<br />")
document.write(time[0] + "<br />")
document.write(time[1] + "<br />")
document.write(time[2] + "<br />")
document.write(time[3] + "<br />")
document.write(time[4] + "<br />")
document.write(time[5] + "<br />")
</script>
JS settimeout(aa(),1000)function aa()//显示字符串计算
{
//算法你自己写吧,这个动动脑子
}
纯JS就能实现。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)