AndroiderQue
AndroiderQue

Reputation: 1

Android: Time and Timezone problems

I get time data about some event from server in UTC. For example, it should happen in 5 hours.

But when user change time of the device (+2 hours, for example), time before an event becomes 3 hours (not 5)

But if user changes not time but time zone, then everything works ok. Then I decided to check timezone offset. Even if I change my time to +2 hours, system still thinks I'm on my timezone's time.

I have the same timezone offset before and after time change.

Then how to deal with this?

I want if user changes time +2 hours, then to still to show these 5 hours before an event, not 3.

I use Calendar.getInstance().time.time for checking my time.

And it happens that system time is 15:00, event will be on 20:00, user changes time to 19:00 and the app shows that event will be in an hour.

Upvotes: 0

Views: 143

Answers (1)

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23339

If you getting the time in UTC then you can compare it with a UTC time (which doesn't go backwards and forwards of course). Using java.time things are easier.

For example:

 Instant fromServer = serverTime.toInstant(ZoneOffset.UTC);
 Instant now = Instant.now();
 long diff = now.until(fromServer, ChronoUnit.HOURS);

Upvotes: 2

Related Questions