Trying to convert a date which is in String format to Java LocalDateTime.
private DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
private String caseStartDate = dateFormat.format(LocalDateTime.now());
LocalDateTime localdatetime = LocalDateTime.parse(caseStartDate);
But ends up having this exception:
java.time.format.DateTimeParseException: Text '01/03/2020 15:13' could not be parsed at index 0
Doesnt this format support for conversion?
You need to use your format inside LocalDateTime::parse as follows:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
String caseStartDate = dateFormat.format(LocalDateTime.now());
System.out.println(caseStartDate);
LocalDateTime localdatetime = LocalDateTime.parse(caseStartDate, dateFormat);
System.out.println(localdatetime);
}
}
Output:
01/05/2020 09:13
2020-05-01T09:13
Also, see how the toString()
method of LocalDateTime
has been overridden:
@Override
public String toString() {
return date.toString() + 'T' + time.toString();
}