我嘗試將字符串轉換爲java上的日期。我閱讀並嘗試從這個網站"Java Date and Calendar examples"的例子,但我仍然無法編譯和運行它。Java:錯誤解析日期字符串到日期對象
這是我的代碼。
package javaapplication5;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class JavaApplication5 {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
String dateInString = "31-08-1982 10:20:56";
Date date = sdf.parse(dateInString);
System.out.println(date);
}
}
我得到這個錯誤。
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.text.ParseException; must be caught or declared to be thrown
我錯過或做錯了什麼?謝謝你的幫助。
是錯誤是如此的清晰,無論你需要將'sdf.parse()'放在try catch或use throws子句中。 –