Show date in current region

I have a date input line:

2012-06-04 15:41:28

I would like to display it correctly for different countries. For example, in Europe we have dd.MM.yyyy, while in the USA we use MM / dd / yyyy.

My current code is as follows:

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 does not work exactly the way I want (I always get a “uniform” result, for example, “June 4, 2012 3:41:28”, even on my phone). What am I doing wrong?

+5
source share
3 answers

Try the following:

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
+2
source

The easiest way to get the current date in the current locale (device language!):

String currentDate = DateFormat.getDateInstance().format(Calendar.getInstance().getTime());

If you want a date in different styles to use getDateInstance(int style):

DateFormat.getDateInstance(DateFormat.FULL).format(Calendar.getInstance().getTime());

: DateFormat.LONG, DateFormat.DATE_FIELD, DateFormat.DAY_OF_YEAR_FIELD .. ( CTRL + , )

:

String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime());
+1

:

   SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
   System.err.format("%30s %s\n", format, sdf.format(new Date(0)));
   sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

. format().

0
source

All Articles