0

I have an issue with java how can convert string 201204121458 to date format 2012/04/12 14:59 using a method ?

Thank you

1 Answers1

1

This code snippet may help:

    String str = "201204121458";

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
    LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

    DateTimeFormatter printer = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm");
    String formattedDateTime = dateTime.format(printer); // "2012/04/12 14:59"
Lyashko Kirill
  • 513
  • 3
  • 14
  • 1
    These terrible date-time classes were supplanted years ago by the modern *java.time* classes defined in JSR 310. Suggesting their use in 2020 is poor advice. – Basil Bourque Jan 20 '20 at 16:47
  • Don't think that it's really important if we speak about converting one date string to another date string. But generally you are right. The code snippet is adjusted. – Lyashko Kirill Jan 20 '20 at 19:08