繁体   English   中英

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>任何想法如何将其表示为字符串?

我发现的唯一解决方案是使用本机函数(在我的情况下是MySql)。 像这样的东西:

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

试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM