1. 程式人生 > >java 24小時倒計時案例

java 24小時倒計時案例

cal tar sys htm 開啟 12月 分鐘 public cat

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


public class Daojishi {
        static String Countdown="";
        
        public static void main(String[] args) {
            //開啟倒計時
            time2();
            System.out.println("Countdown="+Countdown);
        }
        /**
         * 倒計時,設定時間戳
         
*/ private static void time2() { Calendar c = Calendar.getInstance(); c.set(c.get(Calendar.YEAR), (c.get(Calendar.MONTH) + 1), c.get(Calendar.DAY_OF_MONTH)+1, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), c.get(Calendar.SECOND));// 註意月份的設置,0-11表示1-12月 long
endTime = c.getTimeInMillis(); Date date = new Date(); long startTime = date.getTime(); long midTime = (endTime - startTime) / 1000; while (midTime > 0) { midTime--; long hh = midTime / 60 / 60 % 60;
long mm = midTime / 60 % 60; long ss = midTime % 60; System.out.println("還剩" + hh + "小時" + mm + "分鐘" + ss + "秒"); System.out.println( hh+""+mm+""+ss+""); Countdown=hh+""+mm+""+ss+""; try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }

參考:

http://www.cnblogs.com/heizai002/p/6862625.html

http://blog.csdn.net/kookob/article/details/6885383

java 24小時倒計時案例