0

Anytime I have to handle dates/times in java it makes me sad

I'm trying to parse a string and turn it into a date object to insert in a preparepared statement. I've been trying to get this working but am having no luck. I also get the helpful error message when I go to compile the class.

"Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method setDate(int, Date) in the type PreparedStatement is not applicable for the arguments (int, Date)"

Eh WTF?

Here is the offending code.

for(int i = 0; i < flights.size(); i++){

    String[] details = flight[i].toString().split(":"); 
    DateFormat formatter ; 
    formatter = new SimpleDateFormat("ddMMyyyy");
    Date date = formatter.parse(details[1]); 

    PreparedStatement pstmt = conn.prepareStatement(insertsql);
    pstmt.setString(1, details[0]);
    pstmt.setDate(2, date);
    pstmt.setString(3, details[2] + "00");
    pstmt.setString(4, details[3]);
    pstmt.setString(5, details[4]);
    pstmt.setString(6, details[5]);
    pstmt.setString(7, details[6]);
    pstmt.setString(8, details[7]);
    pstmt.setString(9, details[8]);
    pstmt.executeUpdate();

}
MPelletier
  • 16,256
  • 15
  • 86
  • 137
  • 2
    In my experience, try not to create DateFormat objects on a regular basis. They're really expensive time-wise to create. Try creating it outside your loop and reusing it. You'll save a lot of time. – Brent Writes Code Aug 26 '09 at 17:15
  • @BrentNash Your comment is correct. But let's add a reminder than [SimpleDateFormat] is *not* thread-safe. So be sure you *do* create fresh instance in the context of a new thread. Or, even better, use the new java.time package rather than the old date-time classes. – Basil Bourque Jul 03 '15 at 19:17
  • @BasilBourque That original comment was a long time ago. If you asked me today, I would just tell everyone to use JodaTime for everything. – Brent Writes Code Jul 03 '15 at 20:57
  • @BrentNash Sure, I understand. I'm really speaking to the 8,000 other people reading this, not you. – Basil Bourque Jul 03 '15 at 23:33
  • The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. Also, the [Home Page of Joda-Time](https://www.joda.org/joda-time/) reads: *Note that from Java SE 8 onwards, users are asked to migrate to java.time (JSR-310) - a core part of the JDK which replaces this project.* Check [this answer](https://stackoverflow.com/a/67752047/10819573) and [this answer](https://stackoverflow.com/a/67505173/10819573) to learn how to use `java.time` API with JDBC. – Arvind Kumar Avinash Jun 12 '21 at 19:54

3 Answers3

8

PreparedStatement.setDate takes a java.sql.Date, not a java.util.Date.

(Out of interest, how come you're not actually seeing this as a compile-time error? Your life will become a lot easier if you can resolve compilation failures without having to get to that point in a test run...)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
4

My guess is that you mixed java.util.Date and java.sql.Date ...

Guillaume
  • 18,494
  • 8
  • 53
  • 74
1

String to MySQL Date/Time

import java.sql.Date;
import java.sql.Time;

 statement.setDate(4, Date.valueOf("2009-08-26"));
 statement.setTime(5, Time.valueOf("12:04:08"));
sth
  • 222,467
  • 53
  • 283
  • 367