-4

I am trying to convert a string into a date type, so I can save the current time someone last logged in, into my database. The field in the database takes the date type only and not a string representation of it.

Here is my code for getting the current date and formatting it:

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
carrier.setLastUpdateDate(dateFormat.format(date));

Error:

incompatible types: String cannot be converted to Date

cela
  • 2,352
  • 3
  • 21
  • 43
  • 2
    The only way for *that* code to fail to compile with *that* error message, is for parameter to `setLastUpdateDate` to be a `Date`. Since `format` returns a `String`, you'd get that exact compilation error. If parameter to `setLastUpdateDate` is a `Date`, why do anything with `SimpleDateFormat`? Use do `carrier.setLastUpdateDate(new Date());` – Andreas Sep 09 '17 at 18:26
  • 1
    See: [Parse date-only value in ISO 8601 format in java.time (YYYY-MM-DD)](https://stackoverflow.com/q/39706895/642706) – Basil Bourque Sep 09 '17 at 21:38

3 Answers3

2

The field in the database takes the date type only and not a string representation of it.

Your error is clear, you can't set a String in a date type, so instead of convert the date to string set the date like this :

carrier.setLastUpdateDate(new Date());
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
  • 2
    I wonder why this answer was down-voted. I obviously [agree with the answer](https://stackoverflow.com/questions/46133681/convert-a-string-to-date-for-database#comment79229851_46133681). YCF_L, maybe you need to explain more clear, e.g. something like I did in that comment? +1 from me. – Andreas Sep 09 '17 at 18:29
  • 1
    thank you @Andreas i appreciate your fair action yes this is true of what you said in your comment – Youcef LAIDANI Sep 09 '17 at 18:31
2

Parse, not format

The format command generates a string, whereas you are trying to do the opposite, parse a String. So you should have called parse.

Avoid legacy date-time classes

Another problem: You are using troublesome old date-time classes that are now legacy, supplanted by the java.time classes.

Avoid Date, Calendar, SimpleDateFormat, and others not in the java.time package.

Date-only, not date-time

Another problem: You are using a date-time class to represent a date-only value.

The LocalDate class represents a date-only value without time-of-day and without time zone.

ISO 8601

No need to specify a parsing pattern. Your input happens to be in standard ISO 8601 format. The java.time classes use standard formats by default when parsing and generating strings.

LocalDate ld = LocalDate.parse( "2017-01-23" ) ;

Use objects with database, not strings

Another problem: You are using strings to communicate with your database rather than appropriate data types (objects).

Pass the java.time objects to your database directly with a JDBC driver compliant with JDBC 4.2. Call PreparedStatement::setObject.

String sql = "INSERT INTO tbl_ ( when_ ) VALUES ( ? ) ; " ;
PreparedStatement ps = connection.prepareStatement( sql ) ;
ps.setObject( 1 , ld ) ;  // Pass the java.time object directly.

Going the other direction, use ResultSet::getObject.

LocalDate ld = myResultSet.getObject( … , LocalDate.class ) ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
-2

Try this:

DateFormat df = new SimpleDateFormat("MM-dd-yy");
Date d=new Date();
String date1 = df.format(d);
java.sql.Date date2 = new java.sql.Date(d.getTime());
System.out.println(date1);
System.out.println(df.format(date2));
cela
  • 2,352
  • 3
  • 21
  • 43