molleman
molleman

Reputation: 2946

How do i set date formats from one country to another?

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?

Upvotes: 0

Views: 194

Answers (2)

Squonk
Squonk

Reputation: 48871

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).

Upvotes: 0

Sanjay T. Sharma
Sanjay T. Sharma

Reputation: 23208

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);

Upvotes: 1

Related Questions