Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 970
c0dehunter Avatar asked Jun 04 '12 16:06

c0dehunter


1 Answers

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
like image 94
lenooh Avatar answered Sep 26 '22 06:09

lenooh