0

Please find the below program to convert dates from one format to another.

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
          ArrayList<String> list=new ArrayList<String>();//Creating arraylist    
          list.add("02-JAN-2020");//Adding object in arraylist    
          list.add("06-APR-2020");    
          list.add("09-ARR-2020");    
          list.add("07-MAY-2020");    
          
          int j;
           
          //converted array list into string array
          String array[] = new String[list.size()];              
            for( j =0;j<list.size();j++){
              array[j] = list.get(j);
            }
         
          DateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          DateFormat inputFormat = new SimpleDateFormat("dd-MMM-yyyy");
          
         //convert the input format to the output format
          for (int counter = 0; counter < j; counter++) {   
                      
          Date date = inputFormat.parse(array[counter]);
          String outputText = outputFormat.format(date);
          System.out.println(outputText);       
          }   
          
         
    }

}

While running the program, I'm getting this error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type ParseException

Akshaya V
  • 5
  • 3
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Jul 24 '20 at 10:06

1 Answers1

0

You need to throw or use try/catch for parseException

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


 }
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • The error was incorrect date '09-ARR-2020', which should have been '09-APR-2020'. ANd I included exception as well. Working fine. Thanks. – Akshaya V Jul 24 '20 at 11:32
  • @AkshayaV While that is indeed an error, it was not the reason for the message that you asked about, *Exception in thread "main" java.lang.Error: Unresolved compilation problem: Unhandled exception type ParseException*. This answer is correct as answer to your question. – Ole V.V. Jul 25 '20 at 05:19