0

I am trying to initialise a specific date into date object.How can i do that?I tried using the simpledateformat and parse it in from a string,but it gave me a "declare thrown exception" error when i try to run it.

Date joinDate = new Date();
SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
String join = "12/05/2012" ;
joinDate = df.parse(join);
Paul Ang
  • 65
  • 1
  • 1
  • 11

2 Answers2

0
public static void main(String[] args) throws ParseException
    {
        Date joinDate = new Date();
        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String join = "12/05/2012" ;
        joinDate = df.parse(join);

        System.out.println(joinDate);
    }
Maas
  • 1,317
  • 9
  • 20
0

This will work fine. But you need to add try-catch block or throws to method

try{
    Date joinDate = new Date();
    SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    String join = "12/05/2012" ;
    joinDate = df.parse(join);
    System.out.println(joinDate);
}catch(ParseException e){
    // handle the ParseException
}

Or

 public static void main(String[] args) throws ParseException {
     Date joinDate = new Date();
     SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
     String join = "12/05/2012" ;
     joinDate = df.parse(join);
     System.out.println(joinDate);
 }
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115