I have this fragment of code to bidirectional parsing String to LocalDateTime:
public class ConvertLocalDateToString {
private static final DateTimeFormatter CUSTOM_LOCAL_DATE;
static {
CUSTOM_LOCAL_DATE = new DateTimeFormatterBuilder()
.appendValue(YEAR, 4, 10, SignStyle.EXCEEDS_PAD)
.appendLiteral('-')
.appendValue(MONTH_OF_YEAR, 2)
.appendLiteral('-')
.appendValue(DAY_OF_MONTH, 2)
.appendLiteral(' ')
.appendValue(HOUR_OF_DAY, 2)
.appendLiteral(':')
.appendValue(MINUTE_OF_HOUR, 2)
.optionalStart()
.appendLiteral(':')
.appendValue(SECOND_OF_MINUTE, 2)
.toFormatter();
}
public static void main(String[] args) {
String str = "2020-02-18 15:04:30";
LocalDateTime dateTime = LocalDateTime.parse(str, CUSTOM_LOCAL_DATE); //2020-02-18T15:04:30
LocalDateTime addedDateTime = LocalDateTime.parse(dateTime.plusHours(10).format(CUSTOM_LOCAL_DATE.withZone(ZoneOffset.UTC)));
System.out.println(addedDateTime);
System.out.println(dateTime); //2020-02-18T15:04:30
}
My assumption is 'T' letter is auto-generated by ISO-8601 format. This caused:
DateTimeParseException: Text '2020-02-18 15:04:30' could not be parsed at index 10
How do I get rid of it?