用Java计算时间(Android Studio)

时间:2017-08-14 08:06:07

标签: java android countdowntimer valueconverter

我从服务器那里得到一次。这次保存在一个数组中,我在TextView中用适配器中的代码显示这个时间:

txttime.setText(time[position]);

我当前有System.currentTimeMillis()的时间,我想计算System.currentTimeMillis()和我从服务器获得的时间之间的差异。 我想在CountDownTimer()

中使用此结果
new CountDownTimer(result, 1000)

感谢您的建议:)

1 个答案:

答案 0 :(得分:1)

试试这个。认为time1是您当前的时间,而时间2是您获得的服务器时间。

String Time1 = "10/02/14 09:29:50";
        String Time2 = "11/04/14 09:12:43";

            // date format
            SimpleDateFormat format = new SimpleDateFormat("yy/MM/dd HH:mm:ss");  

        Date d1 = null;
        Date d2 = null;
        try {
            d1 = format.parse(Time1);
            d2 = format.parse(Time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }    


        long diff = d2.getTime() - d1.getTime();
        long diffSeconds = diff / 1000;         
        long diffMinutes = diff / (60 * 1000);         
        long diffHours = diff / (60 * 60 * 1000);                      
        System.out.println("Time in seconds: " + diffSeconds + " seconds.");         
        System.out.println("Time in minutes: " + diffMinutes + " minutes.");         
        System.out.println("Time in hours: " + diffHours + " hours.");