Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
769 views
in Technique[技术] by (71.8m points)

android - Formatting a String date into a different format in Java

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

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

My current attempt is the following:

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;
}

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

The converted finalDateString set to your textView


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...