java 實現倒計時的效果 – 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 onload="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/>

 

www.aiwalls.com
</form>


</body>
</html>

摘自 Java編程語言學習專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。