Spring / 休眠。在 Web 服务中将字符串转换为具有相同时区的日期并插入到 SQL

Spring / Hibernate. Converting a String to Date with the same timezone in web service and insert to SQL

我在 json 中有一个网络服务收到的字符串。 Web 服务应该使用字符串上的时间访问 SQL 数据库。代码

String str = "Fri Feb 05 11:00:50 GMT+07:00 2016";

DateFormat dF = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

java.util.Date date = dF.format(str); 

现在 date.toString() 成为 UTC 时区的日期

"Fri Feb 05 04:00:50 UTC 2016"

并且当使用 Spring/Hibernate 保存在数据库中时,它使用 UTC 时间,即使 SQL 不存储时区,因为它是 DateTime。

我目前的技巧是将偏移量添加到新的 Date 对象中,使其变为

"Fri Feb 05 11:00:50 UTC 2016"

即使现在时间不对,保存在 SQL 中也没有关系,因为它不存储时区。

除了我的 hack,还有其他 Date 实用程序(如 sql.datetime 或 joda)可以解决我的问题吗?先谢谢了。

Joda-Time 通过注释和映射支持这一点。我想这就是您要找的东西:Hibernate mapping