有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java总计数未添加到现有列表中

我在java中使用带Rest API的spring boot。我试图获得startDate和endDate之间的所有事件列表,并按startDate排序,但从每个日期仅获得6条记录,而不是更多。如果每个记录日期均为>;6然后我想把计数加到记录的特定日期

假设在这个月里,有n个事件。但我希望每个日期只有六条记录,不需要更多的记录。如果有更多的记录,那么我想增加记录日期的总计数。在这个eventsListTwoDates对象中,我得到了所有事件的列表

我已经从eventsListTwoDates列表中获得了6个记录的每个日期,但如果每个记录日期都是>;六,

一,。JPA自定义查询

  @Query("SELECT new com.techavidus.networkingPhoniex.model.Events(e.id, e.eventImg , e.multipleDays , e.startDate, e.endDate, e.startTime, e.endTime, e.eventStatus, e.paid, e.fee, e.passportTickets, e.location, e.city, e.myFavourite, e.remainingTickets, e.eventName, e.passport, e.typesOfEvents, e.eventPassportOptions, e.promoCode, e.subTitle, e.expectedAttendance)" +
            " from Events e WHERE e.startDate BETWEEN :startDate AND :endDate AND e.endDate BETWEEN :startDate AND :endDate ORDER BY e.startDate")
    List<Events> findAllListOfEventsWithoutEventTypeIdWithOnlyTwoDateTest(String startDate, String endDate);

  1. 密码
public GenericResponse getListEvent()
{
   List<Events> eventsListTwoDates = eventsDao.findAllListOfEventsWithoutEventTypeIdWithOnlyTwoDateTest(firstMondayCurrentMonth, lastDateCurrentMonth);
   Events events = new Events();
   List<Events> eventsList = new ArrayList<>();
   for (int i=0; i<eventsListTwoDates.size(); i++){
       int count = 1;
       for (int j=i+1; j<eventsListTwoDates.size(); j++) {
          if (eventsListTwoDates.get(i).getStartDate().equals(eventsListTwoDates.get(j).getStartDate())){
              count++;
          }
       }
       if (count<=6) {
          events.setCount(count);
          eventsList.add(i,events);
          eventsList.add(eventsListTwoDates.get(i));
          System.out.println(count + " : " +eventsListTwoDates.get(i).getStartDate());
       }
   }
     return APIResponseBuilder.build(true, eventsList, commonMessages.findAllTrue);
}

共 (1) 个答案

  1. # 1 楼答案

    看起来你根本就不会返回count,所以这可能就是为什么你没有它。此外,对于日期评估,我通常会忘记考虑时间部分,因此一些日期函数没有正确评估,我们会遇到类似这样的错误

    我会慢慢调试你的代码,只是为了确定你的函数做了什么。另外,你不想在每个日期范围内循环,而不是在整个列表中循环两次吗?您可以使用Calendar/LocalDateTime类非常容易地获得计数