h2中的CURRENT_TIMESTAMP(以毫秒为单位) [英] CURRENT_TIMESTAMP in milliseconds in h2
问题描述
问题 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:
我要将最初为MySQL编写的应用程序移植到H2 (查询包含一些特定于MySQL的语法).可能是最 使查询在H2上运行具有挑战性的部分是使用 MySQL日期函数,例如DATE(),UNIX_TIMESTAMP()和 FROM_UNIXTIME().好消息是:因为H2支持创建 这些的别名,我能够编写一个很小的Java类, 实现针对H2的这些MySQL函数,一旦我将它们别名为 在这里,H2几乎无需修改即可运行我们所有的查询.
I'm porting an application to H2 that was initially written for MySQL (the queries contain some MySQL-specific syntax). Probably the most challenging part of getting the queries running on H2 has been the use of the MySQL date functions like DATE(), UNIX_TIMESTAMP(), and FROM_UNIXTIME(). The good news is: because H2 supports creating aliases for these, I was able to write a very small Java class that implements these MySQL functions for H2, and once I aliased them into place, H2 runs just about all of our queries without modification.
推荐答案
我知道这是个老问题,但像我这样的人可以作为该问题的搜索答案.我们可以使用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(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!