java 计算时间差,还剩下xx天xx小时xx分xx秒

计算时间差,还剩下xx天xx小时xx分xx秒

使用:

final Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    setDateInfo(NumberUtil.convertTolong(data.getData().getSubmitDate(), System.currentTimeMillis()), orderDateAndNumber);
                    handler.postDelayed(this, 1000);
                }
            };
            runnable.run();
private void setDateInfo(long milliseconds, TextView textView) {
        try {
            Date currDate = new Date(System.currentTimeMillis());
            Date endDate = new Date(milliseconds);
            long diff = currDate.getTime() - endDate.getTime(); // 得到的差值是微秒级别,可以忽略
            long days = diff / (1000 * 60 * 60 * 24);
            long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
            long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
            long seconds = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60) - minutes * (1000 * 60)) / (1000);
            textView.setText("订单将在"
                    + ((2 - (days + 1)) < 0 ? "0" : (2 - (days + 1))) + "天"
                    + (24 - (hours + 1)) + "小时"
                    + (60 - (minutes + 1)) + "分"
                    + (60 - (seconds + 1)) + "秒"
                    + "自动关闭");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java,textview,时间差,时分秒)