How to make JPA Pagination return Date instead of epoch when sorting by date?

Viewed 313

I'm working on a legacy project and I'll try to make it short. I have this Entity that has a field

private Date createdAt;

Then I've defined an implementation in my JpaRepository extension:

Page<Entity> findAllByUserId(Long userId, Pageable pageable);

And when invoking it, I do so:

Sort direction = order != null && order.equalsIgnoreCase("ASC")
      ? Sort.by("createdAt").ascending()
      : Sort.by("createdAt").descending();
PageRequest pageRequest = PageRequest.of(page - 1, size, direction);

So I'm basically sorting them by date. Problem is, if I don't do pagination, it will return the Date as 2019-10-09T04:17:49.000+00:00. That's what I want because that's what everybody using it is expecting it to return. However, whenever I do use pagination, it returns the date as epoch time. I don't really have a problem with it myself, but this is a legacy application and a lot are depending on it so I'd want to change it back into the initial format.

Note: And yes, I know. Date is not recommended it's old I know. But this is a legacy project and I tried changing it to LocalDateTime and unfortunately I wasn't qualified enough as a pasta chef.

0 Answers
Related