SimpleDateFormat 在模拟器上工作正常,但在设备上错误
我使用 SimpleDateFormat 从日期中提取相关信息。它在模拟器中工作得很好,但是在设备上测试时它无法正确格式化。 Logcat 在模拟器中正确显示它,但在手机上再次显示错误。
private String getSectionHeaderTitle(Date date) {
SimpleDateFormat dayFormat = new SimpleDateFormat("E");
SimpleDateFormat monthFormat = new SimpleDateFormat("MMM");
SimpleDateFormat dayNumFormat = new SimpleDateFormat("dd");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
String dayString = dayFormat.format(date);
String monthString = monthFormat.format(date);
String dayNumString = dayNumFormat.format(date);
String yearString = yearFormat.format(date);
String headerTitle = dayString + ", " + monthString + " "
+ dayNumString + " " + yearString;
Log.i(TAG, "Date " + date + " Day " + dayString + ", " + " Month "
+ monthString + " Year " + yearString);
Log.d(TAG, headerTitle);
return headerTitle;
}
模拟器 2.3.3 的日志:
09-09 13:13:55.435: INFO/EventsListActivity(4252): Date 2011-12-11 Day Sun, Month Dec Year 2011
09-09 13:13:55.435: DEBUG/EventsListActivity(4252): Sun, Dec 11 2011
设备 2.3.4 的日志:
09-09 18:30:34.203: INFO/EventsListActivity(7962): Date 2011-10-16 Day 1, Month 10 Year 2011
09-09 18:30:34.203: DEBUG/EventsListActivity(7962): 1, 10 16 2011
非常感谢任何帮助。
I use SimpleDateFormat to pull relevant information from a Date. It worked just fine in the emulator, but when testing on a device it fails to format correctly. The Logcat shows it correctly in the emulator, but again incorrectly for the handset.
private String getSectionHeaderTitle(Date date) {
SimpleDateFormat dayFormat = new SimpleDateFormat("E");
SimpleDateFormat monthFormat = new SimpleDateFormat("MMM");
SimpleDateFormat dayNumFormat = new SimpleDateFormat("dd");
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy");
String dayString = dayFormat.format(date);
String monthString = monthFormat.format(date);
String dayNumString = dayNumFormat.format(date);
String yearString = yearFormat.format(date);
String headerTitle = dayString + ", " + monthString + " "
+ dayNumString + " " + yearString;
Log.i(TAG, "Date " + date + " Day " + dayString + ", " + " Month "
+ monthString + " Year " + yearString);
Log.d(TAG, headerTitle);
return headerTitle;
}
Log for emulator 2.3.3:
09-09 13:13:55.435: INFO/EventsListActivity(4252): Date 2011-12-11 Day Sun, Month Dec Year 2011
09-09 13:13:55.435: DEBUG/EventsListActivity(4252): Sun, Dec 11 2011
Log for device 2.3.4:
09-09 18:30:34.203: INFO/EventsListActivity(7962): Date 2011-10-16 Day 1, Month 10 Year 2011
09-09 18:30:34.203: DEBUG/EventsListActivity(7962): 1, 10 16 2011
Any help is greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SimpleDateFormat 使用的符号(即“Sun”与“1”)是通过 SimpleDateFormat 的 DateFormatSymbols 成员设置的。
在您提供的代码中,您没有显式设置区域设置,因此使用系统的默认区域设置,该区域设置中存储的部分信息是 DateFormatSymbols 对象。
一般来说,相信默认区域设置是正确的从来都不是一个好主意,它可能因系统和其他因素而异。
这正是上面代码中发生的情况,模拟器具有与设备不同的默认区域设置,因此具有不同的 DateFormatSymbols 集。根据您的输出,我猜测设备默认为 Locale.ROOT 语言环境,它只打印出没有语言的整数值。
最简单的修复方法是手动设置 SimpleDateFormat 对象使用的区域设置。
请参阅 http://developer.android.com/reference/java/util/ Locale.html#default_locale 了解可用的区域设置,或者您可以通过 Locale.getAvailableLocales() 方法查看它们。
The symbols (i.e. 'Sun' vs '1') used by SimpleDateFormat are set via the SimpleDateFormat's DateFormatSymbols member.
In the code you've provided, your not explicitly setting the locale, as a result the system's default locale is used, part of the information stored in that locale is the DateFormatSymbols object.
Generally it is never a good idea to trust that the default locale is correct, it can vary between systems and on other factors.
This is exactly whats happening in your code above, the emulator has a different default locale than the device and hence a different set of DateFormatSymbols. Based on your output i'd guess that the device defaults to the Locale.ROOT locale which just prints out the integer values with no language.
The easiest fix it to manually set the locale used for the SimpleDateFormat objects.
See http://developer.android.com/reference/java/util/Locale.html#default_locale for available locales or you can look through them via the Locale.getAvailableLocales() method.