2

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?

starsplusplus
  • 1,232
  • 3
  • 19
  • 32
Harshali
  • 281
  • 4
  • 9
  • 17
  • Please take the time to read through the "JDBC Database Access" trail (http://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?) – MadProgrammer Aug 01 '12 at 05:51
  • 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. – Harshali Aug 01 '12 at 06:00
  • I have date stored in DB as 2014-09-20 format.. it is not getting/parsing a proper date value – mrabro Sep 20 '14 at 20:30

3 Answers3

4

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);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

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);
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
-2
SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");
Date dateValue = null;
try {
    dateValue = date.parse(resultset.getstring(1));
} catch (ParseException ex) {
    Logger.getLogger(pegawai.class.getName()).log(Level.SEVERE, null, ex);
}
jdateChooser.setDate(dateValue); 

note : resultset.getstring(1) is data from database mysql

Abu Yusuf
  • 1
  • 1