问题描述
问题 CURRENT_TIMESTAMP以毫秒为单位讨论了如何从时间戳中减去毫秒MySql
或PostgreSql
".但是答案中的方法不起作用,因为H2不支持像conv
这样的MySQL方法.
The question CURRENT_TIMESTAMP in milliseconds discussed how to "get milliseconds out of a timestamp in MySql
or PostgreSql
". But the methods in the answers don't work because H2 doesn't support MySQL methods like conv
.
我要对架构文件的defaultValueComputed
使用CURRENT_TIMESTAMP
的变体.
I want to use a variant of CURRENT_TIMESTAMP
for the defaultValueComputed
of my schema file.
如何获取H2
中时间戳的确切毫秒数?我希望它是long
.我还希望返回的毫秒数在 Unix时间中.
How can I get the exact milliseconds of a timestamp in H2
? I want it be a long
. I also want the milliseconds returned to be in Unix time.
这里是我的模式文件中:
Here it is in my schema file:
<column name="create_time" type="long" defaultValueComputed="?"
<constraints nullable="false"/>
</column>
我有这个DAO
对象:
public interface MyDao extends Transactional<MyDao> {
@SqlUpdate(
"INSERT INTO my_table "(id, create_time)" +
"VALUES (:id, :create_time)"
void insert(@BindBean MyObject myObject);
}
我查看了 http://www.h2database的时间和日期函数"部分.com/html/functions.html ,尽管PARSEDATETIME
可能可以工作,但找不到任何可以做到这一点的东西.
I looked at the "Time and Date Functions" section of http://www.h2database.com/html/functions.html and couldn't find anything that could do this though maybe PARSEDATETIME
somehow could work.
也许别名是可能的.参见groups.google.com/forum/#!topic/h2-database/kziTTTNlB9o:
Maybe an alias is possible. See groups.google.com/forum/#!topic/h2-database/kziTTTNlB9o:
推荐答案
我知道这是个老问题,但像我这样的人可以作为该问题的搜索答案.我们可以使用h2
DATEDIFF()
函数以毫秒为单位获取当前时间.
I know this is old question but anyone like me, can be search answer for this question. We can get current time by millisecond with h2
DATEDIFF()
function.
首先,我们传递current_timestamp和第一个日期值,并返回类型SECOND
作为参数,例如:DATEDIFF('SECOND', DATE '1970-01-01', CURRENT_TIMESTAMP()) * 1000
返回的结果对我们来说是current_time的毫秒.
Fisrtly we pass current_timestamp and first date value and return type SECOND
as a parameters like :DATEDIFF('SECOND', DATE '1970-01-01', CURRENT_TIMESTAMP()) * 1000
the returned result is current_time's millisecond for us.
这篇关于h2中的CURRENT_TIMESTAMP(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!