2

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?

Martin Nemeth
  • 659
  • 3
  • 16
  • 24

5 Answers5

9

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());
5

The following is what I did when I had a similar problem.

java.util.Date utilDate = new java.util.Date("mm/dd/yyyy");
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

Substitute a real date for "mm/dd/yyyy"

Susie
  • 5,038
  • 10
  • 53
  • 74
0

calendar1.getTime() returns a Date object, and Time is a sub-class of Date, so a Date cannot be cast into a Time. You can try this instead:

Time Cas2 = new Time(calendar1.getTimeInMillis());
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
0
public class Time extends java.util.Date

It is not possible to cast Date to Time.

You can use this instead:

Time time = new Time(date.getTime())
Daniel
  • 1,861
  • 1
  • 15
  • 23
0

You can try this. I've not tested the code.

Time time = new Time(calendar.get(Calendar.MILLISECOND) 
            + calendar.get(Calendar.ZONE_OFFSET));

You might have to subtract the zone offset.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111