javajdatechooser

How to retrieve chosen date from JDatechooser?


I am using Eclipse to do a college project on Java. I realized that java does not have a built in date selector like C#, so I downloaded and added JDateChooser. I tried to retrieve the chosen date but it failed:

 String Date = dateChooser.getDate(); //I want to the date to be retrieved as string

Any ideas? Is there some kind of initialization that I must do?


Solution

  • Retrieve the date that the user selected by calling getDate(), which returns a Date object. Then convert that object into a String by calling SimpleDateFormat.format():

    Date              d;
    SimpleDateFormat  sdf;
    String            s;
    
    d = dateChooser.getDate();                      // Date selected by user
    sdf = SimpleDateFormat("MM/dd/yyyy HH:mm:ss");  // Or whatever format you need
    s = sdf.format(d);                              // Viola
    

    See also: Question 5683728