将MySQL日期转换为Java日期:Android

时间:2014-04-13 13:58:56

标签: java android mysql date

我得到的值是

  

2014年4月13日

来自mysql。

我使用了这段代码

String date = "2014-04-13";


try {
        SimpleDateFormat formatter = new SimpleDateFormat("dd MMM, yyyy", java.util.Locale.getDefault());
    Date convertedDate = (Date) formatter.parse(date);
    Log.d("Date", convertedDate.toString());
} catch (ParseException e) {

    e.printStackTrace();
}

转换不会发生。它提供了一个解析异常,并且Android应用程序也被停止了。

我希望输出为

  

2014年4月4日

1 个答案:

答案 0 :(得分:0)

我得到了它的工作

try {
    SimpleDateFormat readFormat = new SimpleDateFormat( "yyyy-MM-dd", java.util.Locale.getDefault());
    SimpleDateFormat writeFormat = new SimpleDateFormat( "dd MMM, yyyy", java.util.Locale.getDefault());

    java.util.Date convertedDate = readFormat.parse( date );

    String formattedDate = writeFormat.format( convertedDate );

    Log.d("Date", formattedDate);

} catch (ParseException e) {

    e.printStackTrace();
}                   }