Reputation: 1703
After reading the accepted answer to Date formatting based on user locale on android for german, I tested the following:
@Override
protected void onResume() {
super.onResume();
String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (ParseException e) {
// handle exception here !
}
// get localized date formats
DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(getApplicationContext());
String s = dateFormat.format(date);
dateTV.setText(s);
}
Here dateOfBirth is an english date. If I change the phone's language to German however, I see 02.26.1974. According to http://en.wikipedia.org/wiki/Date_format_by_country, the proper localized german date format is dd.mm.yyyy, so I was hoping to see "26.02.1974".
This leads to my question, is there a way to fully localize dates or is this a manual process where I must pore through my app for dates, times, etc.?
Upvotes: 1
Views: 2082
Reputation: 6561
String dateOfBirth = "02/26/1974";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = null;
try {
date = sdf.parse(dateOfBirth);
} catch (Exception e) {
// handle exception here !
}
// get localized date formats
Log.i(this,"sdf default: "+new SimpleDateFormat().format(date)); // using my phone locale
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.US);
Log.i(this,"dateFormat US DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.DEFAULT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE DEFAULT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
Log.i(this,"dateFormat US SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.GERMAN);
Log.i(this,"dateFormat GERMAN SHORT: "+dateFormat.format(date));
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.CHINESE);
Log.i(this,"dateFormat CHINESE SHORT: "+dateFormat.format(date));
output is:
sdf default: 26.02.74 0:00
dateFormat US DEFAULT: Feb 26, 1974
dateFormat GERMAN DEFAULT: 26.02.1974
dateFormat CHINESE DEFAULT: 1974-2-26
dateFormat US SHORT: 2/26/74
dateFormat GERMAN SHORT: 26.02.74
dateFormat CHINESE SHORT: 74-2-26
Upvotes: 4