亲爱的,
我正在使用弹簧数据,实体将 createdDate 保存为日期时间,并在存储库中像这样:
@Query(value="SELECT t FROM MyEntity t WHERE t.createdDate Between ?1 and ?2")
public List<MyEntity> findAllBetweenDates(Calendar from, Calendar to);
所以我不能使用:
setParameter(1,new Date(), TemporalType.DATE
在后端 bean 中,我使用以下内容:
//to set zero of hours,minutes,seconds and milliseconds
fromCalendar.set(java.util.Calendar.HOUR, 0);
fromCalendar.set(java.util.Calendar.MINUTE, 0);
fromCalendar.set(java.util.Calendar.SECOND, 0);
fromCalendar.set(java.util.Calendar.MILLISECOND, 0);
toCalendar.set(java.util.Calendar.HOUR, 0);
toCalendar.set(java.util.Calendar.MINUTE, 0);
toCalendar.set(java.util.Calendar.SECOND, 0);
toCalendar.set(java.util.Calendar.MILLISECOND, 0);
// add 1 days and decrease 1 millisecond
toCalendar.add(java.util.Calendar.DAY_OF_MONTH, 1);
toCalendar.add(java.util.Calendar.MILLISECOND, -1);
allEntities = myEntityRepository.findAllBetweenDates(fromCalendar, toCalendar);
}
它工作正常。