Format Date yyyy-mm-ddTHH: mm: ssZ for dd / MM / yyyy HH: mm [duplicate]

1

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>     

asked by anonymous 20.09.2017 / 21:01

2 answers

2

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.

    
20.09.2017 / 21:11
0

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:

Learn about the new Java 8 date API

    
20.09.2017 / 22:34