以下是来自iOS的一些示例代码:
NSDate *startDateX = [NSDate date];
// Do a bunch of stuff
NSLog(@"Time difference: %f", -[startDateX timeIntervalSinceNow]);输出如下所示:
Time difference: 15.009682在Android/Java中实现这一点的等效方法是什么?
发布于 2012-12-04 02:38:01
您可以使用System.currentTimeMillis()来计算毫秒:
long start = System.currentTimeMillis();
// Do stuff
long difference = System.currentTimeMillis() - start;
Log.v("Time difference:", String.valueOf(difference));如果你想要更精确的东西,可以使用System.nanoTime()。如果你想要秒,只需将System.currentTimeMillis()除以1000即可。
有一个DateUtils类(android.text.format.DateUtils)可以计算相对时间差,比如"5分钟前“。另外,如果你打算花时间做大量的工作,Joda Time也是一个很棒的第三方库。
https://stackoverflow.com/questions/13689475
复制相似问题