我正在尝试在我的应用上制作新闻Feed,其中显示来自不同用户的帖子。
我想在Feed项目上显示的是一个时间戳,表示当前时间与创建帖子的时间之间的差异。
我一直在尝试测试它,但我在logcat中获得的值超过400000.
这是我的代码:
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 :(得分:0)
也许使用DateUtils getRelativeDateTimeString会对你更有用吗? http://developer.android.com/intl/es/reference/android/text/format/DateUtils.html