我正在使用Spring Data,我有一个名为Event
的实体,其字段eventDate
为java.util.Date
。
我想找到一年中的所有活动,所以我首先尝试编写方法签名:
List<Event> findAllByEventDateYear(String symbol, int year);
但当然它不起作用,我的第二次尝试是使用Between:
List<Event> findAllByEventDateBetween(Date dateStart, Date dateEnd);
为了使该方法更有用,我在服务中添加了以下内容:
private List<Event> findAllByEventDateYear(int year) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.DAY_OF_YEAR, 1);
Date dateStart = cal.getTime();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, 11); // 11 = december
cal.set(Calendar.DAY_OF_MONTH, 31);
Date dateEnd = cal.getTime();
List<Event> quotes = eventDao.findAllByEventDateBetween(dateStart, dateEnd);
return events;
}
有没有更好的方法来实现Spring Data?
答案 0 :(得分:0)
如果有人在寻找解决方案
LocalDate instance = LocalDate.now().withYear(year);
LocalDate dateStart = instance.with(firstDayOfYear());
LocalDate dateEnd = instance.with(lastDayOfYear());
eventDao.findAllByEventDateBetween(dateStart, dateEnd);