How to handle time zone difference between server and native android application?

前端 未结 5 1742
孤街浪徒
孤街浪徒 2021-02-19 20:52

Suppose that my server located in USA and I live in Russia. We know that they have different time zones.

My application getting text(String) from server. An

5条回答
  •  半阙折子戏
    2021-02-19 21:16

    There isn't really such as thing as "Russia time zone" or "USA time zone". Both of those countries have several different time zones. See the Wikipedia articles on Time in Russia and Time in the USA.

    You should always write server code such that it is not dependent on the time zone that the server is running in. Usually this is done by storing all time as UTC. Since the client is an Android device, just convert to and from local time on the client, sending just UTC to/from the server.

    If you're working in Java, you should probably use Joda Time for your conversions. It is much cleaner and easier to use than the Calendar class.

    Update

    As Basil pointed out in comments, the Joda-Time project is now in maintenance mode. The team advises migration to the java.time classes defined by JSR 310. For earlier Android, see the ThreeTen-Backport and ThreeTenABP projects. See How to use….

提交回复
热议问题