京东秒杀

@SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            if (msg.what == 0) {
                Calendar instance = Calendar.getInstance();
                int hour = instance.get(instance.HOUR_OF_DAY);
                int tohour = 2;
                if (hour % 2 == 0) {
                    h = hour + tohour;
                } else {
                    h = hour - 1 + tohour;
                }

                int minute = instance.get(Calendar.MINUTE);
                int second = instance.get(Calendar.SECOND);

//        计算时差
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");


                try {
                    Date d1 = simpleDateFormat.parse(h + ":00:00");
                    Date d2 = simpleDateFormat.parse(hour + ":" + minute + ":" + second);
                    //Date   d2 = new   Date(System.currentTimeMillis());//你也可以获取当前时间
                    long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
                    long hours = (diff / (1000 * 60 * 60));
                    long minutes = (diff - hours * (1000 * 60 * 60)) / (1000 * 60);
                    long seconds = (diff - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / 1000;

                    textTime.setText(h + "点场");
                    textShi.setText(hours + ":" + minutes + ":" + seconds);

                } catch (Exception e) {
                }
                handler.sendEmptyMessageDelayed(0, 1000);
            }
        }
    };

你可能感兴趣的:(京东秒杀)