close

程式代碼:  

        DateFormat dateFormat = 
            new SimpleDateFormat("yyyy-MM-dd");           
        Date birth = dateFormat.parse(args[0]);
        Date current = new Date();
        long life = current.getTime() - birth.getTime();
        System.out.println("你今年的歲數為:" + 
                  (life / (1L * 365 * 24 * 60 * 60 * 1000)));

錯誤訊息:  

        unreported exception java.text.ParseException; 

        must be caught or declared to be thrown

 

SimpleDateFormat 的parse方法要捕捉異常,加上try-catch

try
{
   //程式代碼
}
catch(java.text.ParseException e)
{
   System.out.println(e.getMessage());
}

arrow
arrow
    全站熱搜

    Tako軒 發表在 痞客邦 留言(0) 人氣()