0

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?

logbasex
  • 1,688
  • 1
  • 16
  • 22
  • 1
    The T is not there in the LocalDateTime. It's only an artefact of how System.out.println works. If you want a different format, then use one to format your LocalDateTime before you print it. – Dawood ibn Kareem Feb 20 '20 at 03:13
  • 3
    It’s a little hard to see at a glance, but your second call to `LocalDateTime.parse` is not actually passing CUSTOM_LOCAL_DATE as a second parameter. The [one-argument LocalDate.parse method](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/time/LocalDateTime.html#parse%28java.lang.CharSequence%29) expects an ISO 8601 format, including the `T`. – VGR Feb 20 '20 at 03:14

1 Answers1

1

System.out.println(dateTime) is the same as System.out.println(String.valueOf(dateTime)).

String.valueOf(dateTime) is the same as dateTime.toString(), except it can handle null value.

Calling toString() on a LocalDateTime is documented to do this:

Outputs this date-time as a String, such as 2007-12-03T10:15:30.

The output will be one of the following ISO-8601 formats:

  • uuuu-MM-dd'T'HH:mm
  • uuuu-MM-dd'T'HH:mm:ss
  • uuuu-MM-dd'T'HH:mm:ss.SSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSS
  • uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS

The format used will be the shortest that outputs the full value of the time where the omitted parts are implied to be zero.

If you want different format, call format(DateTimeFormatter formatter).


So, to answer your questions:

My assumption is 'T' letter is auto-generated by ISO-8601 format.

The T is no more auto-generated than the - and : characters.

How do I get rid of it?

Call format(...) and specify the format you want, e.g.

System.out.println(dateTime.format(DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss")));
Community
  • 1
  • 1
Andreas
  • 154,647
  • 11
  • 152
  • 247