Java: java.util.date cannot be cast to java.sql.Time

insertjavasqltime

I have time converted from millis and now i would like to make an SQL Insert with that time value. I tried this but it is not working:

        String INSERT_RECORD ="INSERT INTO PRAVIDLA (CAS,DEN,MIESTNOST,STAV) values(?,?,?,?)";
        PreparedStatement pstmt = con.prepareStatement(INSERT_RECORD);

        Calendar calendar1 = Calendar.getInstance(TimeZone.getTimeZone("UTC"));       
        calendar1.setTimeInMillis(milli);
        Time Cas2 = (Time) calendar1.getTime();

        pstmt.setTime(1, Cas2);
        pstmt.setInt(2, DEN);
        pstmt.setString(3, MIESTNOST);
        pstmt.setString(4, STAV);
        pstmt.executeUpdate();

Any suggestions please?

Best Answer

java.sql.Time extends java.util.Date, so you cannot simply cast it.

You can try something like this:

Time time = new Time(date.getTime());