How to get the real UTC Timestamp regardless the device Time (not Timezone)
Asked Answered
W

1

8

Is there a way in Android to get the current timestamp regardless the Time setted in Device Settings?

I dont have a problem with timeoffset which is out of the timezones. My problem is, that if i set a wrong Time in my Device-Settings the Timestamp i get from the System is based on this wrong Time.

Current UTC Timestamp is 1447164873 it is 10.11.2015 14:14:33 and 15:14:33 Local (my Timezone is +1:00)

if i call System.currentTimeMillis() /1000 i get the right Timestamp = 1447164873;

if i go to the Device Settings and set the Time of my Device one Hour to the Future (16:14:33), System.currentTimeMillis()/1000 returns the wrong Timestamp = 1447168473 which is 15:14:33 and 16:14:33 local.

Is there a possibility to get the real UTC Timestamp regardless the Time set on my Device?

Walt answered 10/11, 2015 at 14:25 Comment(2)
Possible duplicate of How can I get the current date and time in UTC or GMT in Java?Anadiplosis
I see... You can't get the real UTC timestamp if your the Time set in the device is wrong.Anadiplosis
S
0

There is no way to get the correct timestamp if the device time is set wrong.

but the way to solve this problem is to create an API that gives the current timestamp in the backend or a simple lambda function.

And use it whenever you need, here API round trip time should be considered to get the accurate timestamp
Or Just hit the API once on the app open and start a counter of your own to calculate the timestamp.

I have used this method in one of my IoT projects to get timestamps in the microcontroller.

Shoelace answered 16/2, 2022 at 14:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.