1

i'm building an android application which have a chat.
in this chat i each message to have its time sent signature.
my question is as follow:
lets say that the time in my country is X. my friend is abroad and his time is X minus 7 hours.
i'm sending him a message at 16:00 local time.
i want to avoid the situation that he will get at 09:00 a message which it signature will be 16:00 (which is a time in future if you're looking in the eyes of that friend in his country).

is there a way that in my phone the message will be written as 16:00 and in his phone it will be written as 09:00 ? i there a way to convert a time to a local time ?

1
  • Hi. Asaf, you can actually show local time i.e. when your are reading at your phone it's assign your local time and when you send it on it's phone it will get converted to it's local time, may be the case that you mention you can manually convert and send message with time Commented Jun 28, 2012 at 12:53

6 Answers 6

2

System.currentTimeMillis() does give you the number of milliseconds since January 1, 1970 00:00:00 UTC. Date object does not save your local timezone. You can use DateFormats to convert Dates to Strings in any timezone:

DateFormat df = DateFormat.getTimeInstance();
df.setTimeZone(TimeZone.getTimeZone("gmt"));
String gmtTime = df.format(new Date());

linked response

Sign up to request clarification or add additional context in comments.

Comments

1

You should keep all time communications using UTC time. Then localize it for display based on the devices current timezone setting.

2 Comments

i must say i didn't quite understand how to do it.. lets say for the example my time in miliseconds since "eposch" is 12345 which is 21:00 01/01/2000 local Israel time. i'm sending the long to America - which is, lets say minus 7 hours. so how can america show 14:00 01/01/2000 from 12345?
@AsafNevo This article explains how to do time zone conversions in java.
0

Use a long to save your time information as milliseconds since "epoch" (which is January 1, 1970, 00:00:00 GMT). It can be retreived with the Date.getTime() method and new Date objects are easily created using the Date(long millis) constructor. The Date objects are then displayed using the local timezone settings on each device.

EDIT:

Epoch is a defined point in time which is expressed differently in different time zones: 1970-01-01 00:00:00 GMT but 1969-12-31 19:00:00 EST. The timestamp is just the number of milliseconds elapsed since that time. So, for example the timestamp 1341169200 corresponds to 2012-07-01 19:00:00 GMT and 2012-07-01 14:00:00 EST.

7 Comments

so if my long is 12345 and is 21:00 and i will display it in another country it can show 14:00 out of the same long ??
Exactly! It is commonly used that way in Android, for example in the protocol for in-app billing.
doesnt need somehow what country to convert it by?
It's just a matter of how to present the time information. Date.toString() uses TimeZone.getDefault() to produce its date string. If you want to explicitly express the time in another time zone, you use a DateFormat object to do so.
but the timezone who sent the message is bigger then the localtime ? how does it know that 1234 equals 21:00 in Israel and 14:00 in America? in the look of America the 1234 time is the future and it will accrue only in 7 hours.. doesn't it have to get the base time somehow as parameter ?
|
0

You will need to save the time zone which your message will be saved in, and transfer it (or send the unix epoch time) and then on the other side make sure you read it in with the Locale time (using the Android documentation for things like http://developer.android.com/reference/java/util/Calendar.html can help).

Comments

0

Take a look at the answer over here:

https://stackoverflow.com/a/6094475/346232

You need to change the time to UTC and then convert on the device to the timezone.

Comments

0

Avoid java.util.Date/.Calendar

The java.util.Date/.Calendar classes bundled with Java (and Android) are notoriously troublesome, flawed in both design and implementation.

Joda-Time

The Joda-Time library is the way to go. This library inspired the java.time package now built into Java 8 (not available on Android).

UTC

As other answers suggested, the best practice (generally) is to keep your business logic and data storage/communication in UTC time zone (which some think of as no time zone or an "anti" time zone). Adjust to a specific time zone only when expected by the user or data-consumer.

Time Zone

The DateTime class in Joda-Time represents a date-time value along with an assigned time zone.

Note that it is best to specify a time zone in all your operations. Otherwise you will be implicitly relying on the JVM’s current default time zone. This is risky because that zone can change – even at runtime at any moment by any code in any thread of any app running within your app’s JVM. And use proper time zone names, never the 3-4 letter codes.

Example Code

Example code in Joda-Time 2.7.

DateTime sent = DateTime.now( DateTimeZone.getDefault() ) ;
DateTime sentUtc = nowMine.withZone( DateTimeZone.UTC ) ; // Generally, use this for your work, including communicating to other threads and apps and such.

When ready to display to the other user, adjust to the expected time zone.

DateTimeZone zone = DateTimeZone.forID( "America/Montreal" ) ;  // Or DateTimeZone.getDefault() if you want to rely on their JVM’s current default. To be absolutely sure of expected time zone, you really must ask the user.
DateTime sentMontréal = sentUtc.withZone( zone );

To generate a textual representation of those date-time objects, search the many Questions and Answers on StackOverflow.com on that subject. Search for terms like "joda" and "DateTimeFormatter" and "DateTimeFormat".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.