0

This might be easy, and I might no see the obvious, but I'm like 1 hour trying to figure out how to convert the date from string to a format I want.

String date_begin = "15-10-2013 10:25:31";
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // This is the format I want
Date startDate;
try {
    startDate = df.parse(date_begin);
    String startDateString1 = df.format(startDate);
    Log.e("New date: ", startDateString1);      
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

And the output is the following:

New date:(2799): 0021-04-04 10:25:31

How d'hell is displaying this output?

4

3 回答 3

3

15-10-2013 10:25:31不是yyyy-MM-dd HH:mm:ss格式。改为解析它dd-MM-yyyy HH:mm:ss

看起来您想更改格式。解决方案是使用两个SimpleDateFormats,一个用于解析,另一个用于格式化输出。像这样:

DateFormat inputFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
startDate = inputFormat.parse(date_begin);
String startDateString1 = outputFormat.format(startDate);
Log.e("New date: ", startDateString1);
于 2013-10-17T09:57:39.807 回答
2

你必须用不同的格式解析日期

SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
于 2013-10-17T09:58:03.803 回答
0

date_begin 和 SimpleDateFormat("yyyy-MM-dd HH:mm:ss") 的格式不同。两者应该是相同的格式。

于 2013-10-17T10:02:05.100 回答