Android中当前日期和过去日期的日期差计时器

时间:2018-09-18 09:17:37

标签: android date timer

我正在努力使当前日期和过去日期有所不同,而现在我已经很完美了。但是我想要的是计时器,如果我的当前时间是1:02,过去的时间是12:02,那么计时器应该给我这样的结果:01:02:02,01:00:03,01:00 :03,01:00:04。 我知道这不是那么复杂的问题,但是我无法找到解决方案。现在,我正在工作的代码如下:

        try {
                Date currentSystemClockTime = new Date(System.currentTimeMillis());

                long diff = currentSystemClockTime() - serverDateTime.getTime();
                long seconds = diff / 1000;
                long minutes = seconds / 60;
                long hours = minutes / 60;
                long days = hours / 24;

                txtTimer.setText(days+"D:"+hours+":"+minutes+":"+seconds+"");

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

预先感谢

1 个答案:

答案 0 :(得分:1)

我已经解决了我的问题,我在这里为其他人提供答案。我制作了一种方法,可以与计时器一起计算以天,小时,分钟和秒为单位的日期差。

日期差的计算方法:

public void checkingCondition(){

        if(ParseUser.getCurrentUser()!=null && ParseUser.getCurrentUser().has("scanTime")){

          final  Date scanDateTime = ParseUser.getCurrentUser().getDate("scanTime");

            Date now = new Date(System.currentTimeMillis());

            try {
                long diff = now.getTime() - scanDateTime.getTime();

                long diffSeconds = diff / 1000 % 60;
                long diffMinutes = diff / (60 * 1000) % 60;
                long diffHours = diff / (60 * 60 * 1000) % 24;
                long diffDays = diff / (24 * 60 * 60 * 1000);

                System.out.print(diffDays + " days, ");
                System.out.print(diffHours + " hours, ");
                System.out.print(diffMinutes + " minutes, ");
                System.out.print(diffSeconds + " seconds.");

                if(diffDays == 0){
                    String hours = "", minutes = "", seconds = "";
                    if(diffHours < 10){
                        hours = "0"+diffHours;
                    }else{
                        hours = ""+diffHours;
                    }
                    if(diffMinutes < 10){
                        minutes = "0"+diffMinutes;
                    }else{
                        minutes = ""+diffMinutes;
                    }
                    if(diffSeconds < 10){
                        seconds = "0"+diffSeconds;
                    }else{
                        seconds = ""+diffSeconds;
                    }
                    txtTimer.setText(hours+":"+minutes+":"+seconds);


                }else{
                    String hours = "", minutes = "", seconds = "";
                    if(diffHours < 10){
                        hours = "0"+diffHours;
                    }else{
                        hours = ""+diffHours;
                    }
                    if(diffMinutes < 10){
                        minutes = "0"+diffMinutes;
                    }else{
                        minutes = ""+diffMinutes;
                    }
                    if(diffSeconds < 10){
                        seconds = "0"+diffSeconds;
                    }else{
                        seconds = ""+diffSeconds;
                    }
                    txtTimer.setText(diffDays+"D "+hours+":"+minutes+":"+seconds);
                }


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

        }
    }

计时器的处理程序方法:

try {
            final Handler timerHandler = new Handler();

            updater = new Runnable() {
                @Override
                public void run() {
                    try {
                        checkingCondition();

                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
                    timerHandler.postDelayed(updater,500);
                }
            };
            timerHandler.post(updater);
        } catch (Exception exp) {
            exp.printStackTrace();
        }