-3

I am currently getting this error and I really don't know why

java.time.format.DateTimeParseException: Text '21:5:20' could not be parsed at index 3
        at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
        at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
        at java.time.LocalTime.parse(LocalTime.java:441)
...

This is the method I use for parsing.

public static ZonedDateTime parse(String fecha, String pattern) {
    DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_TIME;
    LocalTime date = LocalTime.parse(fecha, formatter);

    return ZonedDateTime.of(LocalDateTime.of(LocalDate.now(), date), ZoneId.systemDefault());
  }

I need to return a ZonedDateTime and therefore I'm doing what I'm doing. The error says it seems to read the correct time from the file 21:5:20, which looks valid, but for some reason it fails to parse it.

I really don't know what I'm doing wrong. Similar questions to this were referring to Dates, intead of Time.

I know this seems like a trivial problem, but I would honestly appreciate some help from the Java experts out here. Thank you in advance.

2 Answers2

1

Use the format "H:m:s".

Detail:

The hour, minute, and second in DateTimeFormatter.ISO_LOCAL_TIME are in the format, HH:mm:ss whereas your time string does not have minutes in two digits. The format, "H:m:s" caters to time units in both, single and double-digit.

Demo:

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {

    public static void main(String[] args) {
        System.out.println(parse("21:5:20", "H:m:s"));
    }

    public static ZonedDateTime parse(String fecha, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern, Locale.ENGLISH);
        LocalTime time = LocalTime.parse(fecha, formatter);

        return ZonedDateTime.of(LocalDateTime.of(LocalDate.now(), time), ZoneId.systemDefault());
    }

}

Output in my timezone:

2021-06-27T21:05:20+01:00[Europe/London]

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

The time is formatted incorrectly for ISO_LOCAL_TIME. The hours, minutes and seconds have a fixed width of 2 digits each. They should be left-padded with zeros to ensure two digits. A parseable time would be: 21:05:20

If you can't change the input format, you can create your own DateTimeFormatter:

DateTimeFormatter formatter = new DateTimeFormatterBuilder()
        .appendValue(HOUR_OF_DAY, 2)
        .appendLiteral(':')
        .appendValue(MINUTE_OF_HOUR, 1, 2, SignStyle.NEVER)
        .optionalStart()
        .appendLiteral(':')
        .appendValue(SECOND_OF_MINUTE, 2)
        .toFormatter();

LocalTime date = LocalTime.parse("21:5:20", formatter);

System.out.println(date);

Prints:

21:05:20

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • Well I can't change the input, is there something I can do to be able to parse this time-format? – user22067792 Jun 27 '21 at 19:52
  • what do you mean with handle? btw I thought of just splitting the string at `:` inside the method itself, and just doing ` LocalTime date = LocalTime.of(Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));`. Now unlocked a whole new world of errors. – user22067792 Jun 27 '21 at 20:05