Challenge:How to implement the Localization of date?

I’m in China ,the Challenge of ch18 is to implement Chinese date format.I hava debugged that the date of the Android program is always in America.

Do you want specific patterns or Locale Date Format?
CharSequence chineseDate = DateFormat.format("yyyy年MM月dd日", date);

Check this. it works on the emulator. However, I don’t know on another device.

Hi zannenkiki,

  1. Please let us know whether you use a physical handset or the emulator.
  2. Please check whether the language settings on the physical handset or the emulator has been set to Chinese.
  3. Please share your files.

Here is my solution.

In, I use the class SimpleDateFormat instead of the class DateFormat because the constructor of class SimpleDateFormat has a Locale param.
In your case, you can change the Locale.getDefault() to Locale.CHINA or Locale.CHINESE as a remedy if you are not sure the language settings on the physical handset or the emulator.

Here, “R.string.formatted_date” is the magic for the implementation of the localization.

        String dateFormat = getResources().getString(R.string.formatted_date);
        String dateString = new SimpleDateFormat(dateFormat, Locale.getDefault())

In strings.xml under dir values, this is the default date format.

    <string name="formatted_date">"EEEE, MMM d, yyyy"</string>

In strings.xml under dir values-zh, this is the specified date format for Chinese language settings. I added “日” in this date format because the localization will help add “月” but lost the “日”.

    <string name="formatted_date">"EEEE,MMMd日,yyyy"</string>
1 Like