SimpleDateFormat works correctly on emulator and is wrong on the device

↘锁芯ラ 提交于 2019-12-20 07:18:39

问题


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.


回答1:


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.

Locale locale = Locale.US;  //or whatever you want, see note below

SimpleDateFormat dayFormat = new SimpleDateFormat("E", locale);
SimpleDateFormat monthFormat = new SimpleDateFormat("MMM", locale);
SimpleDateFormat dayNumFormat = new SimpleDateFormat("dd", locale);
SimpleDateFormat yearFormat = new SimpleDateFormat("yyyy", locale);

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.



来源:https://stackoverflow.com/questions/7366035/simpledateformat-works-correctly-on-emulator-and-is-wrong-on-the-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!