**##根据结束时间与系统时间形成倒计时时间差*************


**## //makeHomeDay,makeHomeHous,makeHomeMin,makeHomeSeconds,分别是空间的id,天,时,分,秒**

    private long hour;
    private long min;
    private long day;
    private long sec;
    private Timer mTimer;
    private Handler timeHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (((Activity)mContext).isFinishing()){
                return;
            }
            if (msg.what == 1) {
                computeTime();
                makeHomeDay.setText(String.valueOf(day));//天数不用补位
                makeHomeHous.setText(getTv(hour));
                makeHomeMin.setText(getTv(min));
                makeHomeSeconds.setText(getTv(sec));
                if (sec == 0 && day == 0 && hour == 0 && min == 0) {
                    mTimer.cancel();
                }
            }
        }
    };
	//为秒,分,时,补0位
	private String getTv(long l) {
        if (l >= 10) {
            return l + "";
        } else {
            return "0" + l;//小于10,,前面补位一个"0"
        }
    }
    
    //执行
    
    private void startRun() {
        TimerTask mTimerTask = new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 1;
                timeHandler.sendMessage(message);
            }
        };
        mTimer.schedule(mTimerTask, 0, 1000);
    }

## // Handler进行倒计时

      private void computeTime() {
        sec--;
        if (sec < 0) {
            min--;
            sec = 59;
            if (min < 0) {
                min = 59;
                hour--;
                if (hour < 0) {
                    // 倒计时结束
                    hour = 23;
                    day--;
                    if (day < 0) {
                        // 倒计时结束
                        day = 0;
                        hour = 0;
                        min = 0;
                        sec = 0;
                    }
                }
            }
        }
    }

## //这个百度上可以找的,很多,在此引用一下。稍微修改了一些。

	/***
     * 获取当前日期距离过期时间的日期差值
     * @param endTime
     * @return
     */
    private void dateDiff(String endTime) {
        String strTime = null;
        // 按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:sss");
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
        long nh = 1000 * 60 * 60;// 一小时的毫秒数
        long nm = 1000 * 60;// 一分钟的毫秒数
        long ns = 1000;// 一秒钟的毫秒数
        long diff;
        day = 0;
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = sd.format(curDate);
        try {
            // 获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime()
                    - sd.parse(str).getTime();
            day = diff / nd;// 计算差多少天
            // 计算差多少小时
            hour = diff % nd / nh;
            // 计算差多少分钟
            min = diff % nd % nh / nm;
            // 计算差多少秒
            sec = diff % nd % nh % nm / ns;

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

上一篇:JS基础-特效篇(定时器)05-手表表盘


下一篇:O‘REILLY: Time Converter —— 12小时制转24小时制