I'm developing IM app for android, I want to use "Last seen" for the friends to see when there friends went offline. My Idea is: 1. In the client side, When the user leave the App, it will send to the server a special message containing the time in GMT. 2. in the server side, the server pass the time as is (gmt) to the friends. 3. in the other clients, each client converts the GMT time to his local time. Here is how I built the message to 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 the 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 the 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 we sent GMT time To the server the recipient will convert the GMT to 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);// to 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 to local
Log.d("times--","Local From... Gmt= "+local);
return local;
}
I always got the local time of the sender not my local! what is the problem?