---
title: "html界面定时器的应用"
author: "hanjava"
date: "2011-07-04T09:25:11Z"
tags: []
categories: ["Web前端"]
slug: "html"
draft: false
lastmod: "2011-07-04T09:25:11Z"
id: 1113696
url: "https://hanjava.iteye.com/blog/1113696"
---

public class TimeClass {
	public long s=0;
	
	public String getTime() throws ParseException{
		
		String strTime=null;
		
		//1.页面设置初始时，当前时间(begin)大于定设置时间(end)时会显示出剩余时间和修改按钮
		SimpleDateFormat dfs = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
		Date begin=new Date();
	  	Date end = dfs.parse("2011-07-01 23:59:59");
	  	
	  	long l=end.getTime()-begin.getTime();
	  	long day=l/(24*60*60*1000);
	  	long hour=(l/(60*60*1000));
	  	long hour2=(l/(60*60*1000)-day*24);
	  	long min=((l/(60*1000))-day*24*60-hour2*60);
	  	s=(l/1000-day*24*60*60-hour2*60*60-min*60);
	  	
	  	strTime=hour+":"+min+":"+s;
	  	//以上用于根据时间来控制修改投票
	  	
		return strTime;
	}

//html中调用
<%@page import="tool.TimeClass;"%>
<body>
		<% 
	  	TimeClass tc=new TimeClass();
	  	String interval=tc.getTime();		 
		 %>
 
		<table border="0" width="100%" height="100%" align="center" cellpadding="0" cellspacing="0" class="biankuang">
		<tr>
		<td>
			<span id="showTime" style="display:none">
			<i style="color:blue">投票剩余时间:</i>
			<span id="clock"><%=interval%></span></span>
			<script type="text/javascript" src="djs.js"></script>
			<script type="text/javascript">
				run();//调用djs.js中的方法
			</script>
</body></html>