【问题标题】:Convert date to milliseconds since epoch using Apache Derby使用 Apache Derby 将日期转换为自纪元以来的毫秒数
【发布时间】:2016-11-25 04:23:28
【问题描述】:

是否可以将 TIMESTAMP 字段转换为自纪元以来的毫秒数?

类似:

select
    toEpoch(current_timestamp)
from SYSIBM.SYSDUMMY1;

【问题讨论】:

标签: timestamp derby unix-timestamp epoch


【解决方案1】:

Apache Derby 没有本机函数,但可以创建自己的函数并从数据库中调用它。

首先,创建将转换日期的java方法:

package DbExamples.StoredProcedures;

import java.sql.Timestamp;

public class DateUtilities {
    public static long toEpoch(Timestamp inputDate) {
        if (inputDate == null) {
            return 0L;
        }

        Long result = inputDate.getTime();

        return result;
    }
}

然后通过在数据库上运行以下sql语句将jar文件注入数据库:

CALL SQLJ.REMOVE_JAR('App.StoredProcedures', 0);
CALL SQLJ.INSTALL_JAR('C:\dev\DbExamples\dist\DbExamples.jar', 'App.StoredProcedures', 0);
CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.classpath', 'App.StoredProcedures');

现在通过运行以下 sql 语句在数据库中创建存储过程:

drop function toEpoch;
create function toEpoch(inputDate timestamp)
returns bigint
parameter style java no sql
language java external name 'DbExamples.StoredProcedures.DateUtilities.toEpoch';

现在您可以运行查询了:

select
    toEpoch(current_timestamp)
from SYSIBM.SYSDUMMY1;

select
    toEpoch(cast('2016-07-21 14:50:00' as timestamp))
from SYSIBM.SYSDUMMY1;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-05
    • 2013-08-19
    • 1970-01-01
    • 2017-02-19
    • 2011-08-31
    • 1970-01-01
    • 2020-12-04
    • 2017-02-20
    相关资源
    最近更新 更多