0

Repository

@Query(value = "SELECT * FROM module m WHERE (:name is null or m.name ilike :name||'%') AND " +
            "(:code is null or m.code ilike :code||'%' ) AND (:is_active is null or m.is_active = :is_active)" +
            "AND (:create_date is null or m.create_date between to_date(':first_date','yyyy-MM-dd') and to_date(':second_date','yyyy-MM-dd')) ORDER BY id DESC", nativeQuery = true)
    ArrayList<ModuleEntity> findAllWithFilters(String name, String code, Boolean is_active, Timestamp first_date, Timestamp second_date);

Service

public ArrayList<ModuleEntity> findAllWithFilters(String name, String code, Boolean is_active, Timestamp first_date, Timestamp second_date) {
        return moduleRepository.findAllWithFilters(name, code, is_active, first_date, second_date);
    }

Entity

@Column
@DateTimeFormat(pattern = "yyyy-MM-dd")
@Temporal(TemporalType.TIMESTAMP)
Timestamp create_date;

Controller

@GetMapping(FETCH_MODULES)
    public HashMap<String, Object> getModules(@RequestParam("page") int page,
                                              @RequestParam("limit") int limit,
                                              @RequestParam(value = "code", required = false) String code,
                                              @RequestParam(value = "name", required = false) String name,
                                              @RequestParam(value = "is_active", required = false) Boolean is_active,
                                              @RequestParam(value = "create_date_before", required = false) @DateTimeFormat(pattern ="yyyy-MM-dd") Timestamp first_date,
                                              @RequestParam(value = "create_date_after", required = false) @DateTimeFormat(pattern ="yyyy-MM-dd") Timestamp second_date){
        HashMap<String, Object> response = new HashMap<>();

        if (code != null || name != null || is_active != null || first_date != null || second_date != null){
            ArrayList<ModuleEntity> moduleFilters = moduleService.findAllWithFilters(name, code, is_active, first_date, second_date);

I need to find the range of creation date values ​​(from and to) but I can't convert String to Timestamp type, here is the error message:

Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Timestamp'; Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.sql.Timestamp] for value '2022-12-21']

Please tell me how to convert

1 Answer 1

3

you can use the Timestamp.valueOf() method. For example:

String dateString = "2022-12-27";
Timestamp timestamp = Timestamp.valueOf(dateString);

Configure it according to your requirements.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.