以秒为单位获取两个Joda日期之间的差异

时间:2017-03-16 11:16:44

标签: java android datetime sharedpreferences jodatime

我正在尝试在我的应用中使用joda API来播放Date对象。

在某些活动中,我使用以下代码<{1}}将joda日期时间存储在sharedpreference

 prefsEdit.putLong(context.getString(R.string.last_status_change_time_key) , DateTime.now().getMillis());

现在在其他一些活动中,我提取这个存储的偏好并使用以下代码计算日期之间的差异

long lastStatusChangeTime = objSharedPref.GetAppLongPrefByKey(R.string.last_status_change_time_key);
DateTime now = DateTime.now();
DateTime dateTime = new DateTime(lastStatusChangeTime);
Seconds seconds = Seconds.secondsBetween(now, dateTime);
int n = seconds.getSeconds();

代码总是返回minus中的值,例如-31-12等。

没有正确计算差异。

缺少什么?

2 个答案:

答案 0 :(得分:1)

curl -u admin:admin -X PUT -H 'Content-Type: application/json' -d'{"id":"3604482","type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number":2}}' http://localhost:8080/confluence/rest/api/content/3604482 | python -mjson.tool 的声明是:

curl

为了获得正面结果,curl -u admin:admin http://localhost:8080/confluence/rest/api/content/3965072?expand=body.storage | python -mjson.tool 日期应该是secondsBetween()日期之前的日期。

这是因为secondsBetween(ReadableInstant start, ReadableInstant end) 没有返回绝对值。 在您的示例中start显然在end之前,因此为了获得正值,调用应该是:

secondsBetween()

而不是:

dateTime

您的代码可能是:

now

答案 1 :(得分:0)

只需使用本机Java代码。

long oldTime = Calendar.getInstance().getTime().getTime();
Thread.sleep(10*1000);
long newTime = Calendar.getInstance().getTime().getTime();

long diffInMillisecods = newTime - oldTime;
long diffInSeconds = diffInMillisecods / 1000;