Tuesday, February 2, 2016

Date vs Time vs Timestamp in SQL

java.util.Date

Direct Known Subclasses:
DateTimeTimestamp

  • java.sql.Date         year,month,day
  • java.sql.Time         hour,minute,second
  • java.sql.Timestamp         year,month,day,hour,minute,second
long represents time in milliseconds, starts from 1970/1/1 00:00:00
Example for date into sql:

 
 PreparedStatement ps = con.prepareStatement(sql);

 java.sql.Date date = new java.sql.Date(System.currentTimeMillis());
 ps.setObject(3, date);

 java.sql.Timestamp timestamp = new java.sql.Timestamp(System.currentTimeMillis());
 ps.setObject(4, timestamp);
 ps.execute();

Example for resultSet to date:

 
 java.sql.Time time = rs.getTime("ts");
 java.sql.Timestamp timestamp = rs.getTimestamp("ts");
 java.util.Date date = rs.getDate("ts");


No comments:

Post a Comment