16

I want to store dates in my Cloud database all under one time-zone and then for all my users (whose Android devices could be anywhere in the world), I want that date to be converted and displayed for them with whatever their default Android settings are.

Here it shows how to store dates as a long with Java in code using this: System.currentTimeMillis()

1) However, how do I "normalize" the date to be UTC (this is what I hear everyone saying I should do)?

2) Then, after that date is retrieved from the Cloud DB, how do I with Java convert that UTC date to whatever the time-zone of the device the app is installed on is?

From what I gather, Android might do some of this stuff automatically, but I am not sure what - and I am not sure what I myself have to do. Could someone clarify the above for me?

Thanks.

Community
  • 1
  • 1
Micro
  • 10,303
  • 14
  • 82
  • 120
  • 1
    1/ nothing to do, currentTimeMillis is a timestamp, it does not have a timezone (the timezone is only the stuff you use when you need to display the date to people so you know what time/day it represents). 2/ Again, nothing to do. – njzk2 Oct 13 '15 at 18:37

1 Answers1

9

According to the docs System.currentTimeMillis() returns a UTC timestamp. In other words, assuming the device has synchronized to a time server before you get the timestamp (which is very likely), it will not include a time zone adjustment.

http://developer.android.com/reference/java/lang/System.html#currentTimeMillis()

Similarly, if you retrieve the timestamp and use a Calendar object, the Calendar should adjust to the Locale of the device if you use Calendar.setTimeInMillis(long milliseconds)

http://developer.android.com/reference/java/util/Calendar.html#Calendar()

http://developer.android.com/reference/java/util/Calendar.html#setTimeInMillis(long)

In other words, you really don't have to "normalize" this information, Linux/Java does it for you. What you have to worry about is getting timestamps from email, messages or other systems that have already have a Locale adjustment, correct or not. These are the difficult timestamp problems.

Jim
  • 10,172
  • 1
  • 27
  • 36