[英]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.