QueryDSL:格式化字符串的 DateTimePath

QueryDSL: DateTimePath to formatted string

我需要在 querydsl 中使用 "like" 将日期与字符串进行比较。所以我需要将 datetimepath 转换为特定格式的字符串。在我的例子中是 "MM/dd/yyyy" 类似的东西:

String dateString = "%20/2015%";
QueryBase whereClause = query.where(myEntity.date.toString("MM/dd/yyyy").like(dateString));

myEntity.date 是 DateTimePath<java.util.Date> 任何想法如何将其表示为字符串?

试试这个

public static BooleanExpression getValue(DateTimePath<DateTime> inputDatePath, String ep){
    return inputDatePath.month().stringValue()
            .concat("/")
            .concat(inputDatePath.dayOfMonth().stringValue())
            .concat("/")
            .concat(inputDatePath.year().stringValue())
            .contains(ep); 
}   

query.where(getValue(myEntity.date , "11/20/2015"));

我找到的唯一解决方案是使用本机函数(MySql 在我的例子中)。 像这样:

Expressions.stringTemplate("DATE_FORMAT({0}, {1})", myEntity.date, "%m/%d/%Y")
           .likeIgnoreCase(dateString);