时间范围的去相同数据以及时间注意
public R list(){ Date date = new Date(System.currentTimeMillis()+1000*60*15*4*8); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd‘T‘HH:mm:ss.SSS‘Z‘"); Date oldDate = new Date(date.getTime()-1000*60*15); System.out.println(sdf.format(oldDate)); System.out.println(sdf.format(new Date())); //Query query = new Query(Criteria.where("createTime").lte(oldDate)); Query query = new Query(Criteria.where("createTime").gte(oldDate).lte(date)).with(Sort.by(Sort.Order.desc("createTime")));; List<AisShipRealtimeEntity> result = mongoTemplate.find(query,AisShipRealtimeEntity.class); List<AisShipRealtimeEntity> list= result.stream().collect( Collectors.collectingAndThen( Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(AisShipRealtimeEntity::getShipMmsi))), ArrayList::new) ); System.out.println(list.size()); return R.ok(list); }
主要注意这个时间范围有时间的差异,差开八小时的时区。注意这个时间的问题