Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion Between Android Time vs Java Time

I am having a little bit of trouble with a time conversion between an android phone (Nexus one) and a java server. Everything I have read says that they are the same but when I am converting a long time stamp from i seem to lose 1 hour (exactly).

Specifically, If I run the following code on the android device I get the following output

Code:

Calendar g = Calendar.getInstance();
g.setTimeInMillis(1340661899000L);
Log.d(TAG, g.getTime().toLocaleString());

Output: Jun 25, 2012 6:04:59 PM

Which I think is correct, but when I run the exact same code on a java server I get a the same day but 1 hour earlier

Code:

Calendar g = Calendar.getInstance();
g.setTimeInMillis(1340661899000L);
System.out.println(g.getTime().toLocaleString());

Output: 25-Jun-2012 5:04:59 PM

Does anyone know what could be causing this? Both the server ad the phone are located in the same place (not that it should matter) and the clocks on both the server box and the phone match

like image 918
John S Avatar asked Jun 25 '12 19:06

John S


2 Answers

Considering it's a difference of one hour, could you possible have DST (Daylight Savings Time) set on server and not on the phone or vice-versa?

like image 199
Jason L Avatar answered Nov 08 '22 10:11

Jason L


What Chris is trying to say is that your phone and server may be configured to different timezones.

Try printing the result of cal.getTimeZone(). should this be the issue you will want to pick a timezone to use for communications. For instance:

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+0"));

According to the JavaDocs the calendar instances use the default timezones reported by:

TimeZone.getDefault()

Despite your server and mobile being set to the same TimeZone may not necessarily mean they use the same default TimeZone.

like image 45
ian.shaun.thomas Avatar answered Nov 08 '22 09:11

ian.shaun.thomas