Search code examples
androiddatelocalizationlocale

Show date in current locale


I have an input date-time string:

2012-06-04 15:41:28

I would like to properly display it for various countries. For example, in Europe we have dd.MM.yyyy whereas US uses MM/dd/yyyy.

My current code is like this:

TextView timedate = (TextView) v.findViewById(R.id.report_date);
SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
curFormater.setTimeZone(TimeZone.getDefault());
Date dateObj = curFormater.parse(my_input_string); 
timedate.setText(dateObj.toLocaleString());

But it doesn't work exactly as I want (I always get the "uniform" result like "Jun 4, 2012 3:41:28 PM", even on my phone). What am I doing wrong?


Solution

  • Try this:

    TextView timedate = (TextView) v.findViewById(R.id.report_date);
    SimpleDateFormat curFormater = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
    curFormater.setTimeZone(TimeZone.getDefault());
    Date dateObj = curFormater.parse(my_input_string);
    
    int datestyle = DateFormat.SHORT; // try also MEDIUM, and FULL
    int timestyle = DateFormat.MEDIUM;
    DateFormat df = DateFormat.getDateTimeInstance(datestyle, timestyle, Locale.getDefault());
    
    timedate.setText(df.format(dateObj)); // ex. Slovene: 23. okt. 2014 20:34:45