首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将unix时间戳转换为在java中等效的postgres时间戳。

将unix时间戳转换为在java中等效的postgres时间戳。
EN

Stack Overflow用户
提问于 2014-12-18 01:55:16
回答 2查看 1.5K关注 0票数 0

我在postgres有一个字段

代码语言:javascript
复制
Column     |            Type          
created_at    | timestamp without time zone 

我在Java中将unix时间戳存储在long中

代码语言:javascript
复制
long createdAtTime = data.getcreatedAtTime();

我想将它转换为java中的时间戳,这样我就可以用activejdbc将其存储到postgres中。

我尝试了以下几种方法

代码语言:javascript
复制
Date convertedTime = new Date(createdAtTime*1000L);
record.set("created_at", convertedTime);
record.saveIt();

但我得到以下错误:

代码语言:javascript
复制
Can't infer the SQL type to use for an instance of java.util.Date. Use setObject() with an explicit Types value to specify the type to use.

我应该先使用不同的方式转换日期吗?

EN

回答 2

Stack Overflow用户

发布于 2014-12-18 02:27:55

代码语言:javascript
复制
java.sql.Timestamp timestamp = new Timestamp(createdAtTime*1000L); 
record.set("created_at", convertedTime);
record.saveIt();

而不是util,直到您尝试使用java.sql.Date。它是一种原生类型的SQL。

票数 0
EN

Stack Overflow用户

发布于 2018-10-15 18:24:02

我看到您的数据库列将值存储为不带时区的时间戳,您为什么不尝试这样做

代码语言:javascript
复制
Timestamp current = Timestamp.from(Instant.now());
record.set("created_at", current);//I don't know if you might need to parse
record.saveIt();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27531844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档