1

I tried to see questions about date convert issues between two database using java but didn't solve my problem.

Here is the current date to insert in my database with a DateTime format :

    java.sql.Date SQLDateValue = new java.sql.Date(System.currentTimeMillis()) ;
    preparedStatement.setDate(index, SQLDateValue);

And here is the Timestamp from an API named Vdoc, convert to String and i tried to convert it to java.sql.Date (DateTime) :

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    java.util.Date DateValue = (java.util.Date) this.getWorkflowInstance().getValue(this.ListeChamps[i][2]);
    String StringDateValue = DateValue.toString();
    java.sql.Date SQLDateValue = new java.sql.Date (sdf.parse(StringDateValue).getTime());
    preparedStatement.setDate(index, SQLDateValue);

The second line return a field value containing a String but i need to use toString().

The following error message is :

    Failed to convert the date and / or time from a string.

Both of my date parameters are java.sql.date, i don't understand. If you have an idea of ​​what happens with this, it would be nice to help me.

Ezerah

Sorry for my bad english

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
Ezerah
  • 49
  • 2
  • 2
    Date.toString() do not what you expected. See [here](http://stackoverflow.com/questions/530012/how-to-convert-java-util-date-to-java-sql-date) how you can convert java.util.Date to java.sql.Date – Jens Jul 31 '15 at 08:50
  • 2
    *Why* do you think you need to convert the value to a string and back? What is the value in question? – Jon Skeet Jul 31 '15 at 08:50
  • tell me what : `this.getWorkflowInstance().getValue(this.ListeChamps[i][2])``return before instanciate it to DateValue ? Maybe the problem is from here – uknowbigmams Jul 31 '15 at 08:56
  • @uknowbigmams It returns an IWorkflowInstance, which is an object from the API. It is the value of a field and it's a timestamp date here – Ezerah Jul 31 '15 at 09:14

2 Answers2

1

Just construct the java.sql.Date from java.util.Date.

Call java.util.Date::getTime to extract the count of milliseconds from epoch. Pass that count to constructor of java.sql.Date.

In your case below should work.

java.util.Date DateValue = (java.util.Date) this.getWorkflowInstance().getValue(this.ListeChamps[i][2]);
java.sql.Date SQLDateValue = new java.sql.Date (DataValue.getTime());
preparedStatement.setDate(index, SQLDateValue);
Basil Bourque
  • 262,936
  • 84
  • 758
  • 1,028
sag
  • 5,025
  • 7
  • 46
  • 88
0

try this:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date DateValue = **sdf.parse(**this.getWorkflowInstance().getValue(this.ListeChamps[i][2])**)**;
String StringDateValue = DateValue.toString();
java.sql.Date SQLDateValue = new java.sql.Date (sdf.parse(StringDateValue).getTime());
preparedStatement.setDate(index, SQLDateValue);

You can't cast String to Date, you should parse it

Mark Rotteveel
  • 90,369
  • 161
  • 124
  • 175
  • 1
    No need to generate a string. Just extract the count of milliseconds from epoch of j.u.Date and pass to constructor of j.s.Date. See the [correct Answer](http://stackoverflow.com/a/31742031/642706) by Alexander. – Basil Bourque Aug 01 '15 at 18:30