开发者

Android Time.format("%a") giving incorrect day of the week

I'm trying to retrieve the localized day for a given timestamp. My code is something like this

String timestamp = "2011-08-01T15:55:03.000+02:00";
Time time = new Time();
time.parse3339(timestamp);
// In strftime format "%a" should be the abbreviated weekday name
// according to the current locale.
String result = time.format("%a")

S开发者_运维百科ince the 1st of Aug. 2011 is a Monday the result String should be "Mon" (assuming English locale), but instead it is "Sun"!


time.normalize() must be called, otherwise the values for weekDay, yearDay, isDst and gmtoff will all be 0.

The correct code is:

time.parse3339(timestamp);
time.normalize(false);
String localizedDayOfWeek = time.format("%a")

Thanks to Femi, who put me on the right track by pointing out that in certain cases the time may be converted to UTC by time.parse3339().


You should check the return value: from the javadoc:

Returns true if the resulting time value is in UTC time.

If the parse returned a UTC time that falls on Sunday you might get this. Not sure how that would be possible given your example above, but I can't think of anything else that would do this.


Do you really need to parse the given timestamp, or would you like to get date now? In the case of the latter, use:

Time time = new Time();
time.setToNow();
String result = time.format("%a");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜