0

只是想确认我应该如何向用户显示时间戳。我们以 UTC 格式存储我们的系统时间戳,例如提交日期和时间戳以及某些操作或事件发生的时间等。

我现在正在做一些测试。我进行查询并将UTC时间戳列表作为字符串返回,然后我想将其放入日历表单中。我正在这样做:

someTimeStamp = Calendar.getInstance();
long l = Long.valueOf(stringTimeStampFromDBViaJson);
someTimeStamp.setTimeInMillis(l);

例如,我从 UTC 数据库返回的值是

2012-09-18 19:36:23.950
2012-09-18 19:31:55.193

所以我的手机在 EST 中,直接显示如下

18 September, 2012 03:36:23 PM
18 September, 2012 03:31:55 PM

这就是我想要的。我只是想知道这是否是我可以指望的行为。

4

1 回答 1

2

Unless you explicitly define otherwise, all Java stamps represent UTC time and date since the Unix epoch (Jan 1 1970). If you use System calls (i.e. Android/Google library API calls) then you need to know if these are local or UTC but it's usually easy to tell from the API prototype and, in my experience, is "sufficiently" documented.

I'm working on a project right now which has to mix UTC, GMT and local and all of my stamps are internally presented as milliseconds UTC since the epoch. Only when I display to the user do I take timezones into account. I use the Calendar class exclusively for representing DTGs and for calculations and have found it both accurate and reliable.

I also have a helper class which contains methods such as

long startOfDayUsingPrefs(long millis) 

and

long startOfDay(long millis).  

The former returns the timestamp for the first tick since midnight today UTC or local time, depending on a preference the user sets (use local timezone or UTC). The latter is always UTC.

Hope this helps..

于 2012-09-18T20:05:33.233 回答