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.
Hey,
Do you want specific patterns or Locale Date Format?
CharSequence chineseDate = DateFormat.format("yyyy年MM月dd日", date);
or
Check this. it works on the emulator. However, I don’t know on another device.
Hi zannenkiki,
- Please let us know whether you use a physical handset or the emulator.
- Please check whether the language settings on the physical handset or the emulator has been set to Chinese.
- Please share your CrimeFragment.java/strings.xml/strings.xml(zh) files.
Here is my solution.
In CrimeFragment.java, 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())
.format(mCrime.getDate());
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>
太棒了,您成功解决了我的问题,非常感谢!
@crystalDf @zannenkiki i used a better solution with no string resources required and more flexible to different languages config, here is the code:
public String getLocalizedDateFormatString(Context context, Date date){
Locale currentLocale;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
currentLocale = context.getResources().getConfiguration().getLocales().get(0);
}else{
currentLocale = context.getResources().getConfiguration().locale;
}
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG,currentLocale);
return dateFormat.format(date);
}
I declare this method in Crime class, it worked just fine and i think this is much a easier approach. no hard coded string resource required
Note: the context here i used getApplicationContext as argument. Im not sure if other kind of context is acceptable, feel free to try it out and let us know here
Hi @great_gorilla,
I rewrote your method as follows. The “mDate” is a private attribute already defined in the class Crime. Just FYI.
By the way, I prefer using the string resource since it provides the flexibility like the customized week info in the date.
public String getFormattedDate() {
LocaleListCompat localeListCompat = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration());
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG, localeListCompat.get(0));
return dateFormat.format(mDate);
}