Reputation: 1995
I am using Eclipse Juno, java and MySQL.
I am trying to check if the date is null
before converting it to java.sql.Date
prior to writing it to the DB.
The following works provided the date is not null:
java.sql.Date sqlPackIn = new java.sql.Date(dateBoxArchived.getValue().getTime());
So I am checking for null first:
//TODO check for a null date and handle it for dateBoxArchived and dateBoxPackOut
java.sql.Date sqlDateArchived = new java.sql.Date(System.currentTimeMillis());
if (dateBoxArchived.getValue() != null){
sqlDateArchived = dateBoxArchived.getValue().getTime();
} else {
sqlDateArchived = null;
}
However, I am getting an error (Type mismatch can not convert from long to date
) on sqlDateArchived = (dateBoxArchived.getValue().getTime());
and the recommended fix is to change it to long. I can not find any changed that resolves this.
Your help is greatly appreciated.
Regards,
Glyn
Upvotes: 1
Views: 2972
Reputation: 425013
Some points:
Try this:
java.sql.Date sqlDateArchived = dateBoxArchived.getValue() == null ? null : new java.sql.Date(dateBoxArchived.getValue().getTime());
Upvotes: 3
Reputation: 54242
Date.getTime() returns a long
. Presumably your sqlDateArchived
is a Date
. You can't put a long
in a Date
. Either change sqlDateArchived
to be a long, or create a new Date
like:
sqlDateArchived = new Date(dateBoxArchived.getValue().getTime());
Upvotes: 1
Reputation: 1504
In your code
if (dateBoxArchived.getValue() != null){
sqlDateArchived = dateBoxArchived.getValue().getTime();
convert last line to
sqlDateArchived = new Date(dateBoxArchived.getValue().getTime());`
Upvotes: 0