2012-06-04 12 views
5

私は入力日付時刻文字列があります。表示日付

2012-06-04午後03時41分28秒

私は適切に様々な国のためにそれを表示したいと思います。たとえば、ヨーロッパではdd.MM.yyyy、米国ではMM/dd/yyyyが使用されています。

私の現在のコードは次のようである:

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

しかし、私は(私はいつも「制服」は「2012年6月4日午前3時41分28秒PM」のような結果を取得したいと、それは正確に動作しません。 、私の電話でも)。私は間違って何をしていますか?

答えて

2

この試してみてください:あなたは異なるスタイルで日付を持っているしたい場合はgetDateInstance(int style)を使用

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

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

あなたはこのバリアントを試してみました:

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

:、DateFormat.DAY_OF_YEAR_FIELDなどはあなたがあまりにも時間が必要な場合

を(それらのすべてを見るために、Ctrl +スペースを使用します)。どこでSystem.errを取得しましたか?それはAndroidです。 –

+0

これは単なるサンプルで、私はあなたに 'SimpleDateFormat'メソッドの使用を示しています。希望の動作を得るには 'format()'メソッドを使う必要があります。 – teoREtik

+0

私はformat()関数について知っています。私が知らないのは、現在のロケール(たとえば、米国またはヨーロッパ)を正しくフォーマットする方法を検出する方法です。 –

1

現在のロケール(デバイスロケール!)で、現在の日付を取得する最も簡単な方法を他のスタイル:DateFormat.LONGこれが動作していない

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