如何将Joda Time DateTime对象转换为SQL Server格式的字符串?


问题内容

我使用Java中的Joda-Time库,并将日期和时间存储为org.joda.time.DateTime对象。

我如何可靠地将此DateTime对象转换为将由SQL Server(包括时区)正确解析的String,以便可以在INSERT SQL语句中使用它?


问题答案:

java.sql.Timestamp与一起使用PreparedStatement#setTimestamp()

ps.setTimestamp(1, new Timestamp(dateTime.getMillis()));

请注意,java.sql.Date仅存储日期部分,而不存储时间部分。