Mahesh
Mahesh

Reputation: 1267

DateFormat does not get changed when change locale

I am using this code to format the date according to phone locale

private String localisedDate(String date) {

    DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    try{
    Date d1 = df.parse(date);
    Locale myLocale = Locale.getDefault();
    DateFormat sdf = DateFormat.getDateInstance(DateFormat.SHORT, myLocale);
    StringBuffer sb = new StringBuffer();
    FieldPosition fp = new FieldPosition(DateFormat.YEAR_FIELD);
    sb = sdf.format(d1, sb, fp);
    sb.replace(sb.lastIndexOf("/") ,sb.length(), new SimpleDateFormat("/yyyy").format(d1) );
    Log.e("date",sb.toString());
    return sb.toString();
}catch (Exception e) {
    // TODO: handle exception
    return date;
}
}

This code shows perfect behavior on emulator and on galaxy tab p1000(froyo) But on htc and experia no matter which locale selected shows single format only

i.e. dd/MM/yyyy is there any thing else i need to check for locale while on smartphone? please provide some help. Thanks in advance

Upvotes: 1

Views: 744

Answers (1)

Soumyadip Das
Soumyadip Das

Reputation: 1791

You may check this link, probably you may get some idea from here.

After the code

calendar.setTime(date);

just convert calendar to String.

Upvotes: 1

Related Questions