0

I am trying to convert a user-inputted String into hours, minutes, and seconds stored inside a Date object. Some code was borrowed from StackOverflow that uses a SimpleDateFormat object to parse the user input, but when it tries I get a ParseException.

System.out.println("Enter the start time of the event(hh:mm:ss): ");
String input = sc.next();
SimpleDateFormat f = new SimpleDateFormat("kk-mm-ss");
Date start = new Date();
try {
    start = f.parse(input);
} catch (ParseException e) {
   System.out.println("ERROR: Failed to parse start time.");
   e.printStackTrace();
}

Exception

java.text.ParseException: Unparseable date: "01:02:03"
at java.text.DateFormat.parse(Unknown Source)
at Main.createDialog(Main.java:78)
at Main.displayMenu(Main.java:44)
at Main.main(Main.java:26)
clever_trevor
  • 1,530
  • 2
  • 22
  • 42

1 Answers1

7

Take a look at your format compared to what is been entered...

kk-mm-ss
01:02:03

See the difference? Your format is expecting -, but you're providing :

Change kk-mm-ss to kk:mm:ss

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366