How get accurate UTC timestamp in android

后端 未结 2 377
孤街浪徒
孤街浪徒 2021-01-24 06:26

I do not know which one is right for get UTC time

My code is

System.currentTimeMillis()

for java android

Is the result correct

2条回答
  •  清歌不尽
    2021-01-24 06:58

    On Linux platform, the system clock should be set to UTC. Whether it actually is, and whether it is accurate, is ultimately up to the user.

    Calling System.currentTimeMillis() will give the time in UTC since 1970-01-01T00:00:00Z.

    Is the result correct for international?

    Yes. Provided that the clock is synced with a decent network time source and the user hasn't messed with it.

    Maybe user can change device time and result be different?

    Yes they can. There is not much you can do about it.

    You could attempt to connect to a network time server, but the user could block that, or cause your game to connect to a fake time server. If they "own" the platform that your game runs on, you probably have no way to get guaranteed reliable time.

    (The Wikipedia page on NTP talks about man-in-the-middle attacks. Unfortunately, standard NTP doesn't have a way to deal with that. There is a draft RFC for Network Time Security, but they have been working on it since 2015, and it still hadn't concluded at the time of writing.)

提交回复
热议问题