Java: Error parse date's string to date object -
this question has answer here:
i try convert string date on java. read , try example website "java date , calendar examples" still cannot compile , run it.
this code.
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); } }
and got error.
exception in thread "main" java.lang.runtimeexception: uncompilable source code - unreported exception java.text.parseexception; must caught or declared thrown
what missing or wrong? thank help.
the problem java.text.parseexception
checked exception
a checked exception type of exception must either caught or declared in method in thrown
so... might declare in throws
list
public static void main(string[] args) throws parseexception { /* ... */ }
or should handle it
public static void main(string[] args) { try { date date = sdf.parse(dateinstring); } catch (parseexception e) { // proper thing here try // format or log/rethrow/wrap exception } }
Comments
Post a Comment