0

So i send a Date.toString() to an android phone, the server is in one country, how do i format the time so that it shown relative to the timezone of the phone?

For example, if the server sends 5 o clock CDT to the phone, the phone will then display 10 oclock utc ?

any ideas on how i should go about achieving this?

molleman
  • 2,934
  • 16
  • 61
  • 92
  • Do you have control over how the server formats the time? – Squonk Jul 30 '11 at 15:12
  • yes i have complete control, at the moment i only send the string, what would be the best way to send it , so it can be reconstructed client side? – molleman Jul 30 '11 at 15:24

2 Answers2

1

If sending across a string is not an absolute requirement, an alternate solution would be to send the UTC milliseconds and re-construct the time based on the user timezone.

// on the sever side
final Date serverTime = new Date();
sendToClient(serverTime.getTime());
System.out.println("Time sent by server: " + serverTime);

// on the client
final long ms = receiveFromServer();
final Date clientTime = new Date();
clientTime.setTime(ms);
System.out.println("Time received by client: " + clientTime);
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
0

If you are able to send the UTC time in milliseconds, then take a look at DateUtils.formatDateTime(...) which will format and return a String using local conventions.

Use the DateUtils.FORMAT_XXX constants for the flags parameter to specify which components to include (weekday, year etc).

Squonk
  • 48,735
  • 19
  • 103
  • 135