I have a set of fields which I need to save into my database, my problem is that the format with java and mysql doesn't match. I converted everything to String which works but I need to store the data as Date. As I will need to retrieve information between these two dates.

Can anyone help me?

Thank you

Recommended Answers

All 13 Replies

then cast them to the format you need. what exactly is the problem?

The problem is that when I change the format in PHPMyAdmin to Date, it's giving me this:

com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'Fri Sep 28 10:01:56 BST 2012' for column 'pay1d' at row 1

Java has two Date classes, one in java.util (that's the normal Java Date that connects to Calendar etc) and one in java.sql (that's the SQL-compatible version).
Both classes have methods to get and set their value as a long, so you can use a long value to convert easily between them.

Thank James for the hint. I found a way to convert the date:

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

This gives me the date of the day in the format I want but how will I link it to the date chooser?

To save the date I have this code for now:

  String date = date_s.getDate().toString();

where date_s is the variable name of the datechooser.

Presumably date_s.getDate() returns a java.util.date? In which case you could use code like line2 above. Just don't convert to String.

I have this but still there is an error

sqlDate = date_s.getDate();

Your earlier code had a correct converion (line 2). Use that example to understand the correct way to do this.
ps: "still there is an error"? You should ALWAYS post the complete text of any error messages. Nobody here has the time to guess what the error message could be.

I tried this line after the two line above

Date date = sqlDate.getDate();

However it is asking me to change the variable type to int, I need Date. I am confused.

java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime()); // good
sqlDate = date_s.getDate(); // bad

can you see the method call you left out?

utilDate.getTime() this is the one. But I don't know where I should place it.

I tried sqlDate = date_s.getDate(utilDate.getTime()); but no success

  1. Get the Java Date from the chooser.
  2. get the time (mSecs) from the Java Date
  3. create a new SQL Date using the time (mSecs)

I got itttt!!!


    java.sql.Date date = new java.sql.Date(date_s.getDate().getTime());

Thank you, it's working now

hi all!
i have a problem with jdatechooser, when i added a day with format yyyy-mm-dd (same mysql) it have a error is: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect date value: 'Tue Jan 01 00:01:00 ICT 2013' for column 'day' at row 1

how to fix this error...thank.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.