Android - 如何计算上次看到的内容?

时间:2015-07-17 07:29:55

标签: android time

  

在我的应用程序中,我添加了上次看到的用户登录。我必须从服务器获取数据并从那里获取日期。所以我想像whatsapp一样计算时间,称为lastseen。请帮帮我。

String str1 = response.getString("entry_date");
                        try {
                            Date date = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH).parse(str1);
                            long milliseconds = date.getTime();
                            long millisecondsFromNow = milliseconds - (new Date()).getTime();

                            long curTime = System.currentTimeMillis();
                            long curTimesec = curTime / 1000;
                            long diff = curTimesec - millisecondsFromNow;

                            Calendar cl = Calendar.getInstance();
                            cl.setTimeInMillis(diff);

                            Log.e("DATE", cl.get(Calendar.DAY_OF_MONTH) + ":" + cl.get(Calendar.MONTH) + ":" + cl.get(Calendar.YEAR) + "");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

1 个答案:

答案 0 :(得分:0)

如果您已经有一个日期,并且想要检查从那时起到现在的时间,您可以执行以下操作:

long curTime = System.currentTimeMillis();
long curTimesec = curTime / 1000;  //you can continue to do this if you want to get minutes, hours, days, etc
long diff = curTimesec - lastSeenDatesec; 

希望这有帮助,我没有误解你的问题。