是否可以过滤日期?例如,我只想选择最近30天的数据?
选择应该在现在和现在之间-30天之间。
我希望没有查询的解决方案。
@Service
public class PersonService {
@Autowired
private PersonRepository personRepository;
public Stream<Person> all(Person mysearch){
return personRepository
.findAll(Example.of(mysearch))
.stream()
.map(Person::fromPerson);
}
}
class 人员:
public class Person {
public Integer index;
public String firstname;
public String lastname;
@JsonFormat(pattern="dd.MM.yyyy")
public Date exdate;
public String insnr;
private Person(Integer index, String firstname, String lastname, Date exdate, String insnr){
this.index=index;
this.firstname=firstname;
this.lastname=lastname;
this.exdate=exdate;
this.insnr=insnr;
}
public static Person fromPerson(Person person){
return person == null ? null : new Person(person.getIndex(), person.getFirstname(), person.getLastname(), person.getExdate(), person.getInsnr());
}
}
控制器:
@Autowired
private PersonService personService;
@RequestMapping(value="/person/list/**")
public List<Person> loadPersonList(
@RequestParam(value = "insnr" ,required=false) String insnr) throws ParseException {
mysearch.setInsnr(insnr);
return personService.all(mysearch).collect(Collectors.toList());
}
最佳答案
Calendar cal = Calendar.getInstance();
cal.setTime(dateInstance);
cal.add(Calendar.DATE, -30);
Date dateBefore30Days = cal.getTime();
public Stream<Person> all(Person mysearch){
return personRepository
.findAll(Example.of(mysearch)).stream().filter(person -> person.getExdate().compareTo(dateBefore30Days) > 0)
.map(Person::fromPerson)
.findAny()
.orElse(""); }