【发布时间】:2014-10-06 02:54:23
【问题描述】:
我正在尝试将用户输入的字符串转换为存储在 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();
}
异常
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)
【问题讨论】:
-
那么
kk-mm-ss是如何解析01:02:03的呢? -
踢Buttowski,问题的重点是SimpleDateFormat的参数格式。 @SotiriosDelimanolis kk 代表小时 (1-24),mm 是分钟,ss 是秒。文档在这里:docs.oracle.com/javase/7/docs/api/java/text/…
-
-如何解析:? -
这不是因为那是我的错误。