I am using Java DateTimeFormatter for formatting given time and get difference between given start and end time. But I am getting below exception:
Exception in thread "main" java.time.format.DateTimeParseException: Text '1:23am' could not be parsed at index 0
Below is my code:
String dtStr = "1:23am-1:08am";
String t1 = dtStr.split("-")[0];
String t2 = dtStr.split("-")[1];
DateTimeFormatter format = DateTimeFormatter.ofPattern("hh:mma");
LocalTime time1 = LocalTime.parse(t1, format);
LocalTime time2 = LocalTime.parse(t2, format);
Duration dur = Duration.between(time1, time2);
System.out.println(dur.toMinutes() + " minutes " + dur.toSecondsPart() + " seconds");
I am not sure if I am doing anything wrong. Any help would be appreciated.