在Java中将字符串日期格式化为其他格式

问题描述:

我正在尝试格式化具有date键的JSON格式:

I am trying to format a JSON which has a date key in the format:

日期:"2017-05-23T06:25:50"

date: "2017-05-23T06:25:50"

我当前的尝试如下:

private String formatDate(String date) {
  SimpleDateFormat format = new SimpleDateFormat("MM/DD/yyyy");
  String formattedDate = "";

  try {
    formattedDate = format.format(format.parse(String.valueOf(date)));
  } catch (ParseException e) {
    e.printStackTrace();
  }

  return formattedDate;
}

但是在这种情况下,结果未显示在我的应用程序中,TextView是不可见的,我什么也无法记录.

But in this occasion, the result isn't shown in my app, the TextView is invisible and I couldn't log anything.

我真的尝试了其他解决方案,但对我而言没有成功.我不知道是否是因为传入的值是一个字符串.

I really tried other solutions, but no success for me. I don't know if it's because the incoming value is a string.

使用此代码格式化您的"2017-05-23T06:25:50"

Use this code to format your `2017-05-23T06:25:50'

String strDate = "2017-05-23T06:25:50";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Date convertedDate = new Date();
try {
    convertedDate = dateFormat.parse(strDate);
    SimpleDateFormat sdfnewformat = new SimpleDateFormat("MMM dd yyyy");
    String finalDateString = sdfnewformat.format(convertedDate);
} catch (ParseException e) {
    e.printStackTrace();
}

转换后的finalDateString设置为您的textView