【问题标题】:jooq compare 2 dates - it does not like java.sql.Datejooq 比较 2 个日期 - 它不喜欢 java.sql.Date
【发布时间】:2026-02-10 00:10:02
【问题描述】:

我收到以下代码错误:

Date todayDate = new Date();
SimpleDateFormat dataDateFmt = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat dataTimeFmt = new SimpleDateFormat("HHmmss");
java.sql.Date nowDate = java.sql.Date.valueOf(dataDateFmt.format(todayDate));

selectFrom = DSL.using(connection)
        .select(Msg.MSG.MSGKEY, Msg.MSG.MSGSTS, Msg.MSG.MSGTIT,
                DSL.concat(Msg.MSG.MSGTXT1, Msg.MSG.MSGTXT2, Msg.MSG.MSGTXT3),
                DSL.date(Msg.MSG.MSGCDAT), Msg.MSG.MSGCTIM,
                DSL.date(Msg.MSG.MSGRDAT), DSL.date(Msg.MSG.MSGEDAT), 
                Msg.MSG.MSGLUID)
        .from(Msg.MSG)
        .where(Msg.MSG.MSGFID.equal("SYS")
        .and(Msg.MSG.MSGSTS.equal("NEW"))
        .and(Msg.MSG.MSGTYP.equal("WEBF"))
        .and(Msg.MSG.MSGGRP.equal("ALL"))
        .and(Msg.MSG.MSGSDAT.lt(nowDate)));

我在最后一行得到的错误是“类型字段中的方法 lt(Timestamp) 不适用于参数 (Date)”。我正在做的事情与我看到的 here 非常相似。

【问题讨论】:

    标签: java sql jooq


    【解决方案1】:

    您必须将与Msg.MSG.MSGSDAT 中定义的数据类型相同的数据类型传递给lt

    解决方案(不是很通用,但有效):

                    selectStmt = DSL.using(connection)
                            .select(Msg.MSG.MSGKEY, Msg.MSG.MSGSTS, Msg.MSG.MSGTIT,
                                    DSL.concat(Msg.MSG.MSGTXT1, Msg.MSG.MSGTXT2, Msg.MSG.MSGTXT3),
                                    DSL.date(Msg.MSG.MSGCDAT), Msg.MSG.MSGCTIM,
                                    DSL.date(Msg.MSG.MSGRDAT), DSL.date(Msg.MSG.MSGEDAT), Msg.MSG.MSGLUID)
                            .from(Msg.MSG)
                            .where(Msg.MSG.MSGFID.equal("SYS")
                                    .and(Msg.MSG.MSGSTS.equal("NEW"))
                                    .and(Msg.MSG.MSGTYP.equal("WEBF"))
                                    .and(Msg.MSG.MSGGRP.equal("ALL"))
                                    .and(DSL.date(Msg.MSG.MSGSDAT).lt(nowDate)
                                            .or(DSL.date(Msg.MSG.MSGSDAT).eq(nowDate)
                                                    .and(Msg.MSG.MSGSTIM.ge(nowTime))))
                                    .and(DSL.date(Msg.MSG.MSGEDAT).gt(nowDate)
                                            .or(DSL.date(Msg.MSG.MSGEDAT).eq(nowDate)
                                                    .and(Msg.MSG.MSGETIM.le(nowTime))))
    

    where 子句中的关键是DSL.date(Msg.MSG.MSGSDAT)DSL.date(Msg.MSG.MSGEDAT)

    【讨论】: