Java. SpringBoot Example how to work with dateTime in Specification

Working example

    public static <T, V> Specification<T> findByWithTypeCasting5(String functionName, String paramName, V param, Class<T> entityType) {

        checkParam(paramName, entityType);

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(criteriaBuilder.function(
                        "TO_CHAR",
                        String.class,
                        root.get(paramName),
                        criteriaBuilder.literal("YYYY-MM-DD HH24:MI:SS.000")
                ),
                param
        ));

    }

just with date

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(
                root.<Date>get(paramName),

                param
        ));

or like that

    public static <T, V> Specification<T> findByWithTypeCasting2(String functionName, String paramName, V param, Class<T> entityType) {

        checkParam(paramName, entityType);

        return ((root, query, criteriaBuilder) -> param == null ? criteriaBuilder.conjunction()
                : criteriaBuilder.equal(criteriaBuilder.function("date", LocalDate.class, root.get(paramName)), param));
    }
This entry was posted in Без рубрики. Bookmark the permalink.