2015-11-07 85 views
0

我試圖在我的應用程序上從不同用戶的帖子出現的新聞源。解析創建比較當前時間在幾分鐘內

我試圖在Feed項目上顯示的是一個時間戳,表示當前時間和創建該帖子的時間之間的差異。

我一直在試圖對其進行測試,但我在logcat中得到的是超過40萬

這是我的代碼值:

DateFormat originalFormat = new SimpleDateFormat("EEE MMM DDD HH:mm:ss z yyyy"); 
       try { 
        Log.i("Parse time: " , String.valueOf(posts.getCreatedAt())); 
        Date originaldate = originalFormat.parse(posts.getCreatedAt().toString()); 
        long timeMilliseconds = originaldate.getTime(); 
        Log.i("Server time (s): ", String.valueOf(TimeUnit.MILLISECONDS.toSeconds(timeMilliseconds))); 
        long timeMinutes = TimeUnit.MILLISECONDS.toMinutes(timeMilliseconds); 
        Log.i("Server time (min): ", String.valueOf(timeMinutes)); 
        Calendar calendar = Calendar.getInstance(); 
        long seconds = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis()); 
        Log.i("Current time (s): ", String.valueOf(seconds)); 
        long minutes = TimeUnit.SECONDS.toMinutes(seconds); 
        Log.i("Current time (min): ", String.valueOf(minutes)); 
        long differenceMinutes = minutes - timeMinutes; 
        Log.i("Difference time (min):", String.valueOf(differenceMinutes)); 

       } 
       catch (java.text.ParseException e){ 
        e.printStackTrace(); 
       } 

編輯

這是我的logcat:

11-07 09:20:29.820 19390-19430/? I/Server time (s):: 1420617846 
11-07 09:20:29.820 19390-19430/? I/Server time (min):: 23676964 
11-07 09:20:29.820 19390-19430/? I/Current time (s):: 1446884429 
11-07 09:20:29.820 19390-19430/? I/Current time (min):: 24114740 
11-07 09:20:29.820 19390-19430/? I/Difference time (min):: 437776 
+0

可能的重複[如何計算兩個時間字段之間的時間差,相對於日期更改](http://stackoverflow.com/questions/5172593/how-to-calculate-the-time-兩個時間字段之間的差異與尊重t) – Shivam

回答