我的数据库列数据类型是时间戳。如何使用PreparedStatement或Statement插入当前日期和时间?
我试过这个:
java.util.Date date = new java.util.Date();
System.out.println("Current Date : " + dateFormat.format(date));
pstmt.setDate(9, new java.sql.Timestamp(date.getTime()));
但是表中插入的值是1328847536746.这不对,我使用的是sqlite
答案 0 :(得分:0)
Timestamp
中有一个单独的java.sql
值类。
pstmt.setTimeStamp(9, new java.sql.Timestamp(date.getTime()));
javadoc解释说:
public class Timestamp extends Date
java.util.Date
周围的瘦包装器,允许JDBC API将其标识为SQL TIMESTAMP值。
答案 1 :(得分:0)
pstmt.setTimestamp(9, Timestamp.valueOf("2002-03-13 11:10:15.01"));
答案 2 :(得分:0)
这是我到目前为止用来完成它的代码
Timestamp nextRunTimestamp = null;
if(endDate != null || !endDate.equalsIgnoreCase(""))
{
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
dateFormat.parse(endDate);
Calendar tempDate = dateFormat.getCalendar();
tempDate.set(Calendar.HOUR, nextRunTime.get(Calendar.HOUR));
tempDate.set(Calendar.MINUTE, nextRunTime.get(Calendar.MINUTE));
tempDate.set(Calendar.SECOND, nextRunTime.get(Calendar.SECOND));
tempDate.set(Calendar.MILLISECOND, nextRunTime.get(Calendar.MILLISECOND));
if(nextRunTime.before(tempDate) || nextRunTime.equals(tempDate))
{
nextRunTimestamp = new Timestamp(nextRunTime.getTimeInMillis());
}
}
else
{
nextRunTimestamp = new Timestamp(nextRunTime.getTimeInMillis());
}
statement.setTimestamp(2, nextRunTimestamp);
statement.setInt(3, result.getInt("id"));
statement.executeUpdate();
答案 3 :(得分:0)
DateFormat df = new SimpleDateFormat(yyyy/MM/dd HH:mm:ss); // any Date format
System.out.println("Current Date : " + df.format(new Date()));
pstmt.setDate(9, to_timestamp(df.format(new Date()),'YYYY/MM/DD HH24:MI:SS'));
您可以在此处使用TO_DATE('todayDate', 'YYYY/MM/DD HH24:MI:SS')
或
TO_TIMESTAMP('todayDate', 'YYYY/MM/DD HH24:MI:SS')