0

我正在寻找一种方法来获取今天的日期并传递给 sql 表并保存在那里。调用保存的日期并使用 JODA TIME API 执行一些任务。更改后的 Joda 时间日期到 sql 表并保存在那里并继续处理..

我试过这个方法

//prints todays date
java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
//passes wrong date to the table like 1970-07-01 instead of 2013-03-01
String insert = "INSERT INTO TEST_TABLE VALUES(1,"+sqlDate+")";
pStmt = conn.prepareStatement(insert);
pStmt.executeUpdate();

//converting to joda time 
LocalDate ld = new LocalDate(sqlDate);

//some calculations, and how to convert back to sql date?

我在这里要做的是,一个包含 3 列(id、startdate、finishdate)的表。id 将由用户输入,开始日期应自动输入今天的日期。在使用 joda 时间和完成日期进行一些计算后,将设置为完成日期。

代码

String insert = "INSERT INTO TEST_TABLE VALUES(2,'"+timestamp+"')";

错误

Data type mismatch in criteria expression
//I have created table using MS access
//the format of the date column is Date/Time.
4

1 回答 1

4

您可以在此处使用时间戳。java.sql.Timestampextends java.util.Date,所以你可以用 a 做任何事情,你也可以用 a 做任何java.util.Date事情java.sql.Timestamp

将 LocalDateTime 转换为时间戳

Timestamp timestamp = new Timestamp(localDateTime.toDateTime().getMillis());

但是如果你仍然想转换Timestamp成 java.sql.Date 然后使用这个

java.sql.Date date = new java.sql.Date(timeStamp.getTime());  
于 2013-03-01T10:43:52.993 回答