In the achat project development process, the project requirements regardless of the terminal is what time zone settings, where, the terminal time is correct, post to the server in the data packet inside the time field is required to synchronize with the server, that is, the user bought a new phone, a new tablet, do not do any date time, time zone settings , use the app, and get the time data right.
I designed this, in the app's setting there are 3 variables Org_tablet_tm,org_server_tm and Server_timezone,app start, that is, the time when the line server retrieval and the server's time zone are saved in the Org_ Server_tm and Server_timezone, at the same moment, take the time to save the terminal in Org_tablet_tm.
Define the Now () function, which takes the current device time plus the ORG_SERVER_TM-ORG_TABLET_TM difference.
/** * Always Server-based * @return */public static Date now () {Date w_ret=new date (); W_ret.settime (W_ret.gettime () + (Org_server_ TM-ORG_TABLET_TM)); return W_ret;}
Okay, at this point, although the device terminal time is a mess, as long as the time zone is consistent with the server time zone, use the now () function to get the time at this moment on the server.
However, the problem comes, each user's device time zone is not necessarily consistent with the server, it is possible that they do not adjust the time, or do not check the automatic synchronization time, it is necessary for us to use the code in different time zones in the terminal can also be synchronized with the time on the server.
First look at this datetimeconverttoserver function, the time difference between the current time zone and Greenwich is subtracted from the TM, and when the server is in the same time zone as Greenwich mean, it can be calculated to the server:
/*** * Convert local time to server time, generally before submitting data to the date class of data conversion * @param TM * @return */public static date datetimeconverttoserver (Date tm) {if (tm==null) return null;tm=new Date (Tm.gettime ()-getdifftimezonerawoffsetstd (Timezone.getdefault (). GetID ())); Turn to Greenwich Mean date d=new date (Tm.gettime () +getdifftimezonerawoffsetstd (Server_timezone)); return D;}
Look again at the GETDIFFTIMEZONERAWOFFSETSTD function, which calculates the time difference (in milliseconds) between the specified timezone and the Greenwich time zone:
/*** * Calculates the specified time zone and GMT difference * @param timeZoneId * @return */public static int getdifftimezonerawoffsetstd (String timeZoneId) { C0/>//return Timezone.gettimezone (timeZoneId). Getrawoffset (); TimeZone TZ = Timezone.gettimezone (timeZoneId); Return Tz.getoffset (Gregoriancalendar.getinstance (TZ). Gettimeinmillis ());}
I configure an option in the parameter "whether to convert to terminal time", if the conversion, do not do anything, because the Android system can be based on the current time zone automatically converted time, if not converted, display server-side time, you need to usedatetimeconverttoserver function to convert it a bit.
==================================
before the end of Spit groove, the network spread a time zone difference calculation function, they use timezone of getrawoffset, then I also copied to use, various time zones to try again, start is a variety of suitable, but test to Asia/baku when immediately petrified, also is East 5 area of Karachi, The Ural is no problem, since Baku began all the way west of the time zone is a difference of one hour!!! Use getrawoffset to get Baku 4 hours from Greenwich, but it should be 5 hours!! because of the time problem did not carefully analyze what reason, there is free to see and.
Reprint Please specify source: Http://blog.csdn.net/rocklee
Android and server communication: How to ensure both sides time consistency