2017-08-14 219 views
-1

我從服務器獲得了一次。這一次,保存在數組中,我在適配器顯示此時的TextView與此代碼:用Java計算時間(Android Studio)

txttime.setText(time[position]); 

我有當前時間System.currentTimeMillis(),我想計算System.currentTimeMillis()和我的時間,從服務器得到不同。 我想在CountDownTimer()

new CountDownTimer(result, 1000) 

謝謝你的建議:)

+0

從您的服務器的時間是怎麼樣的? –

回答

0

試試這個使用這個結果。認爲time1是你當前的時間,time2是你得到的服務器時間。

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."); 
+0

我在服務器上的時間的風格是=>「1502917200」 我該如何改變這種風格爲您的風格=>「10/02/14 09:29:50」 – mohammad

+0

您必須要求您的後端開發人員以正確的格式 –