11

as の日付形式を変更したい

String date ="29/07/13";

しかし、* Unparseable date: "29/07/2013" (at offset 2) * I want to get date in this format 29 Jul 2013 のエラーが表示されます。

フォーマットを変更するために使用しているコードは次のとおりです。

tripDate = (TextView) findViewById(R.id.tripDate);
    SimpleDateFormat df = new SimpleDateFormat("MMM d, yyyy");
            try {
                oneWayTripDate = df.parse(date);
            } catch (ParseException e) {

                e.printStackTrace();
            }
            tripDate.setText(oneWayTripDate.toString());
4

3 に答える 3

40

このようにしてみてください:

String date ="29/07/13";
SimpleDateFormat input = new SimpleDateFormat("dd/MM/yy");
SimpleDateFormat output = new SimpleDateFormat("dd MMM yyyy");
try {
    oneWayTripDate = input.parse(date);                 // parse input 
    tripDate.setText(output.format(oneWayTripDate));    // format output
} catch (ParseException e) {
    e.printStackTrace();
}

これは 2 段階のプロセスです。まず、既存の文字列を解析して Date オブジェクトにする必要があります。次に、Date オブジェクトを新しい文字列にフォーマットする必要があります。

于 2013-07-29T07:37:02.213 に答える
8

フォーマット文字列をMM/dd/yyyy, while に変更し、 whileparse()を使用dd MMM yyyyformat()ます。

サンプル :

String str ="29/07/2013";
// parse the String "29/07/2013" to a java.util.Date object
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(str);
// format the java.util.Date object to the desired format
String formattedDate = new SimpleDateFormat("dd MMM yyyy").format(date);
于 2013-07-29T07:34:17.677 に答える