1

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.?

Community
  • 1
  • 1
MarcusH
  • 1,693
  • 2
  • 15
  • 20
  • But you'd defined a format yourself in `new SimpleDateFormat("MM/dd/yyyy");` the only difference in formatted date result is that dot used instead of slash. Also you did not used Locale anywhere in code. – Stan Apr 08 '15 at 22:35
  • @Stan getDateFormat returns a localized format: http://developer.android.com/reference/android/text/format/DateFormat.html#getDateFormat(android.content.Context). I am trying to find code that would localize "fully", rearrange the numbers, not just replace with dots in this case. – MarcusH Apr 09 '15 at 00:15

1 Answers1

3
    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
Stan
  • 6,511
  • 8
  • 55
  • 87