I get in JSON a date field "created_at": "2013-01-08T20: 11: 48Z" and wanted to display on the screen in Brazil, but I can not use SimpleDateFormat to format and display in any way. / p>
I get in JSON a date field "created_at": "2013-01-08T20: 11: 48Z" and wanted to display on the screen in Brazil, but I can not use SimpleDateFormat to format and display in any way. / p>
Follow the solution below:
String dataJson = "2013-01-08T20:11:48Z".replaceAll("T", " ").replaceAll("Z", "");
SimpleDateFormat format = new SimpleDateFormat("yyyy-mm-dd HH:mm:ss");
Date dataFormatada = new Date(format.parse(dataJson).getTime());
System.out.println(dataFormatada);
Visit here to see the result.
Try using the new Java 8 date API as follows:
public static void main(String[] args) {
String data = "2013-01-08T20:11:48Z";
DateTimeFormatter originalFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
DateTimeFormatter targetFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
// Com isso já da pra fazer várias manipulações interessantes
LocalDateTime dateTime = LocalDateTime.parse(data, originalFormat);
// ou assim
DateTimeFormatter formatador = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(new Locale("pt", "br"));
System.out.println(data);
System.out.println(dateTime.format(targetFormat));
System.out.println(dateTime.format(formatador));
}
See working at Ideone .
Other references: