我试图将用户输入的String转换为Date对象中存储的小时,分钟和秒.一些代码是从StackOverflow借来的,它使用SimpleDateFormat对象来解析用户输入,但是当它尝试时我得到一个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();
}
Run Code Online (Sandbox Code Playgroud)
例外
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)
Run Code Online (Sandbox Code Playgroud)
与输入的内容相比,查看您的格式...
kk-mm-ss
01:02:03
Run Code Online (Sandbox Code Playgroud)
看到不同?您的格式是期待的-,但您提供的是:
更改kk-mm-ss到kk:mm:ss
| 归档时间: |
|
| 查看次数: |
1533 次 |
| 最近记录: |