2

I have two jDateChooser on my dialog , I want to save to MS-SQL DB having issue with that data types. Any idea how to fix this issue ! I can only do this when i convert data type to nvarchar in DB and convert the value to string which returns from jDateChooser.

enter image description here

// I can save in this way but I it doesn't use jDateChooser;

   java.util.Date utilDate = new java.util.Date();
   java.sql.Date sqldate = new java.sql.Date(utilDate.getTime());

// I cant save the date with jDateChooser

    java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate());

// Only Way I found

  SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

  String sd = dateFormat.format(jDateChooser3.getDate());

  obj.setStartDate(sd);

//

user3498019
  • 95
  • 1
  • 2
  • 10
  • Where is this `JDateChooser` class coming from? Which library and package? – icza May 09 '14 at 12:25
  • it is this one http://toedter.com/software/ – user3498019 May 09 '14 at 12:31
  • Please post MCVE (Minimal Complete and Verifiable Example). By the mean time please check the return type of JdateChooser.getDate() if it is java.util.Date then try java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate()getTime()); – A Stranger May 09 '14 at 12:34

3 Answers3

5

Judging from the code you posted it looks like jDateChooser3.getDate() returns a java.util.Date instance while the java.sql.Date(millis) constructor expects the date/time as a long milliseconds value.

Use this code and it will work:

java.sql.Date sqldate = new java.sql.Date(jDateChooser3.getDate().getTime());

Since it comes from a date chooser component, invalid input most likely results in null returned date, so you might want to also check on that:

java.util.Date d = jDateChooser3.getDate();
if (d == null) {
    System.out.println("No date specified!");
} else {
    java.sql.Date sqldate = new java.sql.Date(d.getTime());
    // Do something with sqldate
}
icza
  • 389,944
  • 63
  • 907
  • 827
0

Right click on jDateChooser. Go to the Properties and dateFormatString. Set this format: dd-MM-yyyy.

JustALady
  • 1
  • 1
0
      Date keyword = jDateChooserattendance.getDate();
      java.sql.Date sqldate = new java.sql.Date(keyword.getTime());