I'm getting the "DateTimeParseException" when parsing the given String:
I know using 'yyyy' and 'dd' are suggested on some questions, but i can't use them. I use JDK 17
import java.time.format.DateTimeFormatter;
import java.time.*;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYYMMDDhhmmss");
LocalDateTime date = LocalDateTime.parse("20120823151034", formatter);
Result:
Exception in thread "main" java.time.format.DateTimeParseException: Text '20120823151034' could not be parsed at index 0
Not only 'yyyy' and 'dd', you also need to use 'HH' instead of 'hh' because 'hh' makes sense only with am/pm marker i.e. for a 12-hour time format. For a time with an am/pm marker, you need to use 'hh' along with 'a' e.g. 'hhmmss a'. Check the documentation to learn more about them.
Output from a sample run:
Online Demo
Learn more about the modern Date-Time API from Trail: Date Time.