我正在尝试检索给定时间戳的本地化日期。我的代码是这样的
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")
由于 2011 年 8 月 1 日是星期一,因此结果字符串应为“Mon”(假设为英语语言环境),但实际上却是“Sun”!
最佳答案
必须调用time.normalize(),否则weekDay、yearDay、isDst和gmtoff的值都将为0。
正确的代码是:
time.parse3339(timestamp);
time.normalize(false);
String localizedDayOfWeek = time.format("%a")
感谢 Femi,她指出在某些情况下时间可以通过 time.parse3339() 转换为 UTC,从而使我走上正轨。
关于Android Time.format ("%a")给出错误的星期几,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6899850/