[Error] Parameter value [2017/08/22] did not match expected type [java.util.Date (n/a)]

entity에 Date형인 orderDate가 존재한다고 했을 때 Specification을 이용한 between 검색을 하는 방법이다. 

물론 interface에서 바로 해도 되지만 복합적으로 조건을 처리해줘야할 때 Specification은 유용하게 사용된다.


파라미터로 fromDt="2017/08/23" , toDt="2017/08/23" 받고 단순히 아래와 같이 했을 때 제목과 같은 오류가 났다. 


public static Specification<OrderList> betweenDate(String fromDt, String toDt) {

return (root, query, cb) -> {

if (fromDt.equals("") && toDt.equals(""))

return null;

return cb.between(root.get("orderDt"), fromDt, toDt);

};

}


Parameter value [2017/08/23] did not match expected type [java.util.Date (n/a)]

말 그대로 Date형에 2017/08/23은 맞지 않는다는 뜻..


public static Specification<OrderList> betweenDate(String fromDt, String toDt) {

return (root, query, cb) -> {

if (fromDt.equals("") && toDt.equals(""))

return null;

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd hh:mm");

Date fromDate = null;

Date toDate = null;

try {

fromDate  = dateFormat.parse(fromDt+ " 00:00");

toDate = dateFormat.parse(toDt+ " 23:59");

} catch (ParseException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return cb.between(root.<Date>get("orderDate"),fromDate ,toDate);

};

}


위와 같이 해주면 뭔가 되긴 하는데 깔끔한 맛은 없다..-_-깔끔한건 다음에 찾아보도록..............