ParseException Java

北战南征 提交于 2019-12-01 16:57:48

Parse Exception is checked exception so you must have to handle it. Either by throws or try catch block.

public static void main (String[] args)

Should be

public static void main (String[] args) throws ParseException

Or in try catch block

try {
    //All your parse Operations
} catch (ParseException e) {
   //Handle exception here, most of the time you will just log it.
   e.printStackTrace();
  }

Why you're getting the error:

How to Fix it:

Surround the offending code with try catch - it's like saying, I will capture the error, log it and hopefully be able to do something about it.

 try {  // add this line
             System.out.print("\n\n\tEnter START Date in mm/dd/yyyy format: ");
             SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
             Date lowDate = sdf.parse(stdin.nextLine());
             System.out.print("\n\n\tEnter END Date in mm/dd/yyyy format: ");  
             Date highDate = sdf.parse(stdin.nextLine());  

             for(int i = 0; i < list.size(); i++)
             {
                int dateSpot = list.get(i).indexOf(" ");
                String currentDate = list.get(i);
                currentDate.substring(0, dateSpot);
                Date newCurrentDate = sdf.parse(currentDate); 

                if (newCurrentDate.compareTo(lowDate) >= 0 && newCurrentDate.compareTo(highDate) <= 0)
                {
                   System.out.println("\n\t" + list.get(i));   
                }
             }
         } catch (ParseException ex) {
              ex.printStackTrace(); // or log it using a logging framework
         }

or throw it at the main - here, it's like saying: whoever it is that's calling this method, please take care of this problem if it happens:

public static void main (String[] args) throws Exception
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!