2012-06-04 8 views
5

Mam data-czas ciąg wejściowy:Pokaż datę w bieżącej lokalizacji

2012-06-04 15:41:28

chciałbym prawidłowo wyświetlić go do różnych krajów. Na przykład w Europie mamy dd.MM.rrrr, podczas gdy USA używa MM/dd/rrrr.

Mój bieżący kod wygląda następująco:

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()); 

Ale to nie działa dokładnie tak, jak chcę (I zawsze uzyskać "uniform" Wynik jak "Jun 4, 2012 3:41:28 PM" , nawet na moim telefonie). Co ja robię źle?

Odpowiedz

2

Spróbuj tego:

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 
0

Czy spróbować ten wariant:

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

dostał go od dokumentacji. Mam na myśli użycie metody format().

+0

to nie działa. Skąd masz System.err ?? To jest Android. –

+0

To była tylko próbka, w której pokażę ci użycie metod 'SimpleDateFormat'. Aby uzyskać życzeniowe zachowanie, musisz użyć metody 'format()'. – teoREtik

+0

Wiem o funkcji format(). Nie wiem, jak wykryć bieżące ustawienia regionalne (np. USA lub Europa), aby poprawnie sformatować. –

1

Najprostszym sposobem, aby uzyskać aktualną datę w bieżącej lokalizacji (locale urządzenie!):

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

Jeśli chcesz mieć datę w różnych stylach używać getDateInstance(int style) :

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

Inne style: DateFormat.LONG, DateFormat.DATE_FIELD, DateFormat.DAY_OF_YEAR_FIELD itp (użyj Ctrl + Spacja, aby zobaczyć wszystkie z nich)

Jeśli potrzebujesz czasu, TOO:

String currentDateTime = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,DateFormat.LONG).format(Calendar.getInstance().getTime()); 
Powiązane problemy