java android - System.currentTimeMillis() 没有按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11954985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:04:15  来源:igfitidea点击:

android - System.currentTimeMillis() does not work as expected

javaandroid

提问by Jens Ehrlich

I use System.currentTimeMillis() to save the time a user starts an activity.

我使用 System.currentTimeMillis() 来节省用户开始活动的时间。

public class TimeStamp {
protected long _startTimeMillis = System.currentTimeMillis();

public String getStartTime() {
    return new Time(_startTimeMillis).toString();
}

the class is instantiated when activity is started and getStartTime() returns the correct time. I also try to get the time that has passed after the activity has been started.

该类在活动开始时实例化并且 getStartTime() 返回正确的时间。我还尝试获取活动开始后经过的时间。

public String getElapsedTime() {
    return new Time(System.currentTimeMillis() - _startTimeMillis).toString();
}

This works perfectly fine using an emulated android device (android 4.0.3). But when I deploy the application on my real android device (android 4.0.3) getElapsedTime() starts with one additional hour and then counts up normally. So on my real device getElapsedTime() will return "01:00:01" after the activity was started, but it should return "00:00:01".

使用模拟的 android 设备(android 4.0.3)可以很好地工作。但是当我在我的真实 android 设备 (android 4.0.3) 上部署应用程序时,getElapsedTime() 开始增加一小时,然后正常计数。所以在我的真实设备上 getElapsedTime() 将在活动开始后返回“01:00:01”,但它应该返回“00:00:01”。

Do you have any ideas why this happens?

你知道为什么会发生这种情况吗?

回答by tolgap

You should use SystemClock.uptimeMillis().

你应该使用SystemClock.uptimeMillis().

uptimeMillis() is counted in milliseconds since the system was booted and is guaranteed to be monotonic whereas System.currentTimeMillis() isn't because the user or an application can change it at any time. Also, usually automatic time synchronization is enabled which can change the time at any time.

uptimeMillis() 自系统启动以来以毫秒为单位计数,并保证是单调的,而 System.currentTimeMillis() 不是因为用户或应用程序可以随时更改它。此外,通常启用自动时间同步,可以随时更改时间。

回答by Laurent Perrin

You can't use Time to represent a difference between 2 instants. In your code, getElapsedTime() returns a time close to 0, which is interpreted as 1970, January 1st. I think that the reason why you have a problem is because you don't have the same time zone on your device, hence the origin of time is not midnight.

您不能使用 Time 来表示 2 个瞬间之间的差异。在您的代码中,getElapsedTime() 返回接近 0 的时间,这被解释为 1970 年,1 月 1 日。我认为您遇到问题的原因是您的设备上没有相同的时区,因此时间的起源不是午夜。

回答by Shivan Dragon

System.currentTimeMillis() returns milliseconds passed between the device's clock time and the epoch date. That's why it can vary from device to device.

System.currentTimeMillis() 返回设备时钟时间和纪元日期之间传递的毫秒数。这就是为什么它会因设备而异。

http://developer.android.com/reference/java/lang/System.html#currentTimeMillis()

http://developer.android.com/reference/java/lang/System.html#currentTimeMillis()