java 实现倒计时的效果

package com.niit;


import java.util.Calendar;
import java.util.Date;


// 实现线程的两种方式:1. 继承Thread类  2. 实现Runnable
// 实现run方法
// 如何启动线程? 线程对象.start();
public class Countdown2 implements Runnable
{


/**
* @param args
*/
public static void main(String[] args)
{
Thread cd = new Thread(new Countdown2());
cd.start();
}


@Override
public void run()
{
// 设置日期2012-12-21
Calendar c = Calendar.getInstance();
c.set(2012, 12, 21, 0, 0, 0);
// 单独设置年、月、日、小时、分钟、秒
c.set(Calendar.YEAR, 2012);
c.set(Calendar.MONTH, Calendar.DECEMBER); // 0 表示1月,11 表示12月
c.set(Calendar.DAY_OF_MONTH, 21);
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);


// 获取2012-12-21 0:0:0时间点对应的毫秒数
long endTime = c.getTimeInMillis();
// 获取系统当前时间
Date now = new Date();
// 获取当前时间点对应的毫秒数
long currentTime = now.getTime();


// 计算两个时间点相差的秒数
long seconds = (endTime - currentTime) / 1000;


while (true)
{
System.out.println("还剩: " + seconds + " 秒");
seconds--;
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}

}


比较一下在js中的效果


<html>


<head>
<b>今天距离2012年1月1日还有</b><br />
<script type="text/javascript">
    var DifferHour = -1;
    var DifferMinute = -1;
    var DifferSecond = -1;
    var Tday1 = new Date("1 1,2012 20:00:00");
    var daysms = 24 * 60 * 60 * 1000;
    var hoursms = 60 * 60 * 1000;
    var Secondms = 60 * 1000;
    var miscrosecond = 1000;
    function clock() {
        var time = new Date();
        var hour = time.getHours();
        var minute = time.getMinutes();
        var second = time.getSeconds();
        var timevalue = "" + ((hour > 12) ? hour - 12 : hour)
        timevalue += ((minute < 10) ? ":0" : ":") + minute;
        timevalue += ((second < 10) ? ":0" : ":") + second;
        timevalue += ((hour > 12) ? "PM" : "AM")
        var convertHour = DifferHour;
        var convertMinute = DifferMinute;
        var convertSecond = DifferSecond;
        var Diffms = Tday1.getTime() - time.getTime();
        DifferHour = Math.floor(Diffms / daysms);
        Diffms -= DifferHour * daysms;
        DifferMinute = Math.floor(Diffms / hoursms);
        Diffms -= DifferMinute * hoursms;
        DifferSecond = Math.floor(Diffms / Secondms);
        Diffms -= DifferSecond * Secondms;


        var dSecs = Math.floor(Diffms / miscrosecond);


        if (convertHour != DifferHour)
            document.formnow.dd.value = DifferHour
        if (convertMinute != DifferMinute)
            document.formnow.hh.value = DifferMinute
        if (convertSecond != DifferSecond)
            document.formnow.mm.value = DifferSecond
        document.formnow.ss.value = dSecs;
        document.formnow.ss.value = dSecs;
        setTimeout("clock()", 1000);
        
    }


</script>
</head>
<body οnlοad="clock() ;return true" text="red">


<form name="formnow">


<input  name="dd" type="text" style="border:0;" size=2/>

<input  name="hh" type="text" style="border:0;" size=2/>
小时
<input  name="mm" type="text" style="border:0;" size=2/>

<input  name="ss" type="text" style="border:0;" size=2/>





</form>


</body>
</html>

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页