Search code examples
javasqljooq

jooq compare 2 dates - it does not like java.sql.Date


I am getting an error with the below code:

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)));

The error I am getting for the last line is "The method lt(Timestamp) in the type Field is not applicable for the arguments (Date)". I am doing something very similar to what I see here.


Solution

  • You must pass the same datatype to lt that is defined in Msg.MSG.MSGSDAT.

    Solution (not very generic, but works):

                    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))))
    

    The key is DSL.date(Msg.MSG.MSGSDAT) and DSL.date(Msg.MSG.MSGEDAT) in where clause.