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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/elsdnwn/article/details/50686964

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

使用:

final Handler handler = new Handler();
       Runnable runnable = new Runnable() {
           @Override
           public void run() {
               setDateInfo(milliseconds, textView);
               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();
        }
    }
展开阅读全文

没有更多推荐了,返回首页