我正在开发一个将 24 小时时间戳转换为 12 小时时间戳的程序。我设法完成转换并循环它,但我需要在检查不正确输入的输入验证中编码。错误输入的示例是:“10:83”或“1):*2”。有人可以告诉我如何使用 Exception 方法来解决这个问题吗?到目前为止,我有这个:
public class conversion {
public static void timeChange() throws Exception {
System.out.println("Enter time in 24hr format");
Scanner sc = new Scanner(System.in);
String input1 = sc.nextLine();
DateFormat df = new SimpleDateFormat("HH:mm");
DateFormat df2 = new SimpleDateFormat ("hh:mm a");
Date date = null;
String timeOutput = null;
date = df.parse(input1);
timeOutput = df2.format(date);
System.out.println("in 12 hour format: " + timeOutput);
decision();
}
public static void decision() throws Exception {
System.out.println("Would you like to enter another …Run Code Online (Sandbox Code Playgroud)