-2

I try to convert string to date on java. I read and try the example from this website "Java Date and Calendar examples" but I still cannot compile and run it.

This is my 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 I got this error.

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported exception java.text.ParseException; must be caught or declared to be thrown

What I missing or do it wrong? Thank you for help.

chin8628
  • 446
  • 7
  • 15

5 Answers5

1

The problem is that java.text.ParseException is checked exception

A checked exception is a type of exception that must be either caught or declared in the method in which it is thrown

So... You might declare it in throws list

public static void main(String[] args) throws ParseException {
    /* ... */
}

Or should properly handle it it

public static void main(String[] args) {
    try {
        Date date = sdf.parse(dateInString);
    } catch (ParseException e) {
        // do proper thing here like try another
        // format or log/rethrow/wrap exception
    }
}
vsminkov
  • 10,912
  • 2
  • 38
  • 50
  • Thanks for including both ways it can be done. It's also good that you quickly explained checked exceptions since it reveals the cause of the compilation error. – byxor Sep 07 '16 at 14:10
  • 1
    @BrandonIbbotson thanks to you :) – vsminkov Sep 07 '16 at 14:11
  • 1
    @BrandonIbbotson Well flagging as duplicate would be best, but who would say "no" to some easily farmed reputation, right? :P – Tom Sep 07 '16 at 14:13
0

Just add your code in try catch block.

try {
 //parse code
}
catch (ParseException e) {
 //handling exceptions
}
mcemilg
  • 976
  • 1
  • 11
  • 18
0

Try to put all your code in a try/ catch block

try{
     // your code here.
 }catch(Exception e){
     e.printStackTrace();
 }
Ray Lloy
  • 171
  • 12
0

Add a try-catch block like this:

try
{
    //...Code.... Mainly below:
    Date date = sdf.parse(dateInString);
} catch (ParseException e) 
  //Or any superclass, such as RuntimeException, 
  //although use superclass ONLY to be generic, not otherwise, such as 
  //if you want to handle all exceptions the same way.
{
    //...Handler Code...
}
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
0
  SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
            String dateInString = "31-08-1982 10:20:56";
            Date date = null;
            try {
                date = sdf.parse(dateInString);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(date);
bhanu avinash
  • 484
  • 2
  • 16