How can I set the date to the jDateChooser which is retrieved from the database?

45,344

Solution 1

If you stored the date in the database as String then you're going to need to retrieve it as String

String dateValue = resultset.getString(...); // What ever column
java.util.Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateValue);

jDateChooser.setDate(date);

Solution 2

If the date chooser that you've mentioned is this one http://plugins.netbeans.org/plugin/658/jdatechooser-1-2 then one possible solution might be this.

String dateValue = resultset.getString(2); // Your column Name
java.util.Date date = new SimpleDateFormat("dd-MM-yyyy").parse(dateValue);

Calendar cal = Calendar.getInstance();
cal.setTime(date);
jDateChooserDCC.setSelectedDate(cal);
Share:
45,344

Related videos on Youtube

Harshali
Author by

Harshali

Updated on February 12, 2020

Comments

  • Harshali
    Harshali over 4 years

    I have a form on which I want to access a date from the database and show in jDateChooser for a particular record.

    I saved the date as a string in the database.

    How do I get the date from the database table and how do I set that date in jDateChooser?

    • MadProgrammer
      MadProgrammer almost 12 years
      Please take the time to read through the "JDBC Database Access" trail (docs.oracle.com/javase/tutorial/jdbc/index.html). Which JDateChooser are you using? What format is the date value in, in the database? (Why did you store it as String?)
    • Harshali
      Harshali almost 12 years
      I was getting exception while I was saving date using 'Date' data type.I am using Jcalendars JdateChooser.Date is in dd-MM-YYYY format.
    • Rafi Abro
      Rafi Abro over 9 years
      I have date stored in DB as 2014-09-20 format.. it is not getting/parsing a proper date value