android - Local and GMT Time don't give a correct result -
i'm developing im app android, want use "last seen" friends see when there friends went offline. idea is: 1. in client side, when user leave app, send server special message containing time in gmt. 2. in server side, server pass time (gmt) friends. 3. in other clients, each client converts gmt time local time. here how built message send:
if (status.tolowercase().equals("offline")) { status =estools.getgmttimadate(system.currenttimemillis());//important entryactivity.currentopenchatid = "none"; } log.d("offline--","will send status = "+ status); onlinemsg.putextra("action", "com.__.message"); string nowtime = "" + system.currenttimemillis(); onlinemsg.putextra(constantsgcm.typeclm, constantsgcm.onst); onlinemsg.putextra(constantsgcm.status_on_of,status);// if offline time in gmt zone onlinemsg.putextra(constantsgcm.to_clm, "-01"); onlinemsg.putextra("v",ver); final string msgid= uuid + nowtime; onlinemsg.putextra(constantsgcm.name_clm,username); final bundle bndl = onlinemsg.getextras();
and getgmttimadate() function code:
public static string getgmttimadate(long currentlocal) { simpledateformat gmtsdftime = new simpledateformat("dd/mm/yyyy - ( hh:mm:ss )"); gmtsdftime.settimezone(timezone.gettimezone("etc/utc")); string gmttime=gmtsdftime.format(currentlocal); //currentlocal=system.currenttimemillis() log.d("times--","gmt= "+gmttime); return gmttime; }
now sent gmt time server recipient convert gmt localtime:
string localfromgmt=estools.getlocalfromgmt(intent.getstringextra(constantsgcm.status_on_of));
the localfromgmt() function code is:
public static string getlocalfromgmt(string gmt) { long timeinmilliseconds=0; date localtime= null; try { simpledateformat gmtsdftime = new simpledateformat("dd/mm/yyyy - ( hh:mm:ss )"); gmtsdftime.settimezone(timezone.gettimezone("etc/utc")); localtime = (date) entryactivity.gmtsdftime.parseobject(gmt);// parse gmt time timeinmilliseconds = localtime.gettime(); // gmt in milliseconds } catch (parseexception e) { e.printstacktrace(); } simpledateformat localsdftime = new simpledateformat("dd/mm/yyyy - ( hh:mm:ss )"); string local= entryactivity.localsdftime.format(timeinmilliseconds);// convert local log.d("times--","local from... gmt= "+local); return local; }
i got local time of sender not local! problem?
Comments
Post a Comment