title: how can I find last 7 days data using spring data JPA?
body: <p>I want to find last 7 orders form order table, how am I supposed to do that? </p>
  
  <p>Here's the order table </p>
  
  <pre><code>CREATE TABLE `orders` (
    `id` bigint(20) NOT NULL AUTO_INCREMENT,
    `total` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    `service_charge` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    `vat` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    `delivery_charge` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    `subtotal` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    `point_used` int(5) DEFAULT NULL,
    `discount_id` bigint(20) DEFAULT NULL,
    `user_id` bigint(20) NOT NULL,
    `address_id` bigint(20) NOT NULL,
    `payement_type` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
    `payment_status` varchar(10) COLLATE utf8mb4_unicode_ci NOT NULL,
    `message` mediumtext COLLATE utf8mb4_unicode_ci,
    `transaction_id` varchar(50) COLLATE utf8mb4_unicode_ci NOT NULL,
    `invoice` enum('yes','no') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'no',
    `status` enum('pending','accepted','processed','on-way','delivered','cancelled') COLLATE utf8mb4_unicode_ci NOT NULL,
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `address_id` (`address_id`),
    KEY `user_id` (`user_id`)
  ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
  </code></pre>
  
  <p>this is the service I'm using </p>
  
  <pre><code>public List&lt;Order&gt; getLastSevenDaysOrders(User user, String todayString, String sevenDaysBeforeString) {
          ArrayList&lt;Order&gt; orders = new ArrayList&lt;&gt;();
          orderRepository.findByUserAndCreatedAtBetween(user, todayString, sevenDaysBeforeString).forEach(orders::add);
          return orders;
      }
  </code></pre>
  
  <p>This is the repository I've.</p>
  
  <pre><code>List&lt;Order&gt; findByUserAndCreatedAtBetween(User user, String todayString, String sevenDaysBeforeString);
  </code></pre>
  
  <p>This is my controller</p>
  
  <pre><code>@RequestMapping(value = "/order/recent", method = RequestMethod.POST)
      public JsonNode getRecentOrder(HttpServletRequest httpServletRequest, @RequestBody String userId) throws IOException {
  
          long startTime = System.currentTimeMillis();
          utility.printCallingUrl(httpServletRequest);
  
          JSONObject request = new JSONObject(userId);
  
          JSONArray response = new JSONArray();
  
          //Order order = new Order();
          List&lt;Order&gt; orders = new ArrayList&lt;&gt;();
  
          String auth = httpServletRequest.getHeader("Authorization");
          String authentication = utility.getBasicAuth();
  
          if (auth.equals(authentication)) {
              try {
  
  
  
                  Calendar calendar = Calendar.getInstance();
                  String pattern = "yyyy-MM-dd HH:mm:ss";
                  DateFormat dateFormat = new SimpleDateFormat(pattern);
                  Date today = calendar.getTime();
                  String todayString = dateFormat.format(today);
  
                  calendar.add(Calendar.DAY_OF_MONTH, -7);
                  Date sevenDaysBefore = calendar.getTime();
                  String sevenDaysBeforeString = dateFormat.format(sevenDaysBefore);
  
                  User user = userService.getUser(request.getLong("user_id"));
  
                  orders = orderService.getLastSevenDaysOrders(user, todayString, sevenDaysBeforeString);
  
                  if (orders.size() &gt; 0) {
                      for (Order order : orders) {
                          JSONObject response2 = new JSONObject();
                          response2.put("receiversName", order.getAddress().getReceiversName());
                          response2.put("address", order.getAddress().getAddress());
                          response2.put("nearbyLandmark", order.getAddress().getNearbyLandmark());
                          response2.put("receiversContact", order.getAddress().getReceiversContact());
                          response2.put("district", order.getAddress().getDistrict().getNameBn());
                          response2.put("division", order.getAddress().getDivision().getNameBn());
                          response2.put("village", order.getAddress().getVillage());
                          response2.put("name", order.getUser().getName());
                          response2.put("userId", order.getUser().getId());
                          response2.put("upazilla", order.getAddress().getUpazilla().getNameBn());
                          response2.put("vat", order.getVat());
                          response2.put("deliveryCharge", order.getDeliveryCharge());
                          response2.put("discountAmount", order.getDiscount().getAmount());
                          response2.put("discountCode", order.getDiscount().getCode());
                          response2.put("discountName", order.getDiscount().getName());
                          response2.put("discountId", order.getDiscount().getId());
                          response2.put("pointUsed", order.getPointUsed());
                          response2.put("message", order.getMessage());
                          response2.put("transactionId", order.getTransactionId());
                          response2.put("createdAt", order.getCreatedAt());
                          response2.put("total", order.getTotal());
                          response2.put("serviceCharge", order.getServiceCharge());
                          response2.put("subtotal", order.getSubtotal());
                          response2.put("orderId", order.getId());
                          response2.put("invoice", order.getInvoice());
                          response2.put("contact", order.getUser().getContact());
                          response2.put("payementType", order.getPayementType());
                          response2.put("paymentStatus", order.getPaymentStatus());
                          response2.put("status", order.getStatus());
  
                          response.put(response2);
                      }
  
                      Log log = utility.saveLog(KeyWord.GET_ORDERS, KeyWord.CMS, httpServletRequest, "200", request, new JSONObject().put("data", response), startTime);
  
                      if (log != null) {
                          utility.showMessage("Log inserted successfully with ID " + log.getId());
                      } else {
                          utility.showMessage("Log not inserted");
                      }
                  } else {
                      response = new JSONArray();
                      Log log = utility.saveLog(KeyWord.GET_ORDERS, KeyWord.CMS, httpServletRequest, "500", request, new JSONObject().put("data", response), startTime);
  
                      if (log != null) {
                          utility.showMessage("Log inserted successfully with ID " + log.getId());
                      } else {
                          utility.showMessage("Log not inserted");
                      }
                  }
  
              } catch (Exception e) {
                  utility.showMessage(e.toString());
                  return mapper.readTree(utility.createResponse(500, KeyWord.ERROR, e.toString()).toString());
              }
  
          } else {
              return mapper.readTree(utility.createResponse(500, KeyWord.ERROR, KeyWord.AUTHENTICATION).toString());
          }
  
          return mapper.readTree(response.toString());
      }
  </code></pre>
  
  <p>Order model</p>
  
  <pre><code>@Entity
  @Table(name = "orders")
  public class Order {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      @Expose
      private long id;
      @Column(name = "total")
      @Expose
      private String total;
      @Column(name = "service_charge")
      @Expose
      private String serviceCharge;
      @Column(name = "vat")
      @Expose
      private String vat;
      @Column(name = "delivery_charge")
      @Expose
      private String deliveryCharge;
      @Column(name = "subtotal")
      @Expose
      private String subtotal;
      @Column(name = "point_used")
      @Expose
      private int pointUsed;
  
      // @Column(name = "discount_id")
      // private long discountId;
  
      @ManyToOne
      @JoinColumn(name = "discount_id")
      @Expose
      Discount discount;
      @ManyToOne
      @Expose
      User user;
      @OneToOne
      @Expose
      Address address;
  
      @Column(name = "payement_type")
      @Expose
      private String payementType;
      @Column(name = "payment_status")
      @Expose
      private String paymentStatus;
      @Column(name = "message")
      @Expose
      private String message;
      @Column(name = "transaction_id")
      @Expose
      private String transactionId;
      @Column(name = "invoice")
      @Expose
      private String invoice;
      @Column(name = "status")
      @Expose
      private String status;
      @Column(name = "created_at")
      @Expose
      private String createdAt;
      @Column(name = "updated_at")
      @Expose
      private String updatedAt;
  
      public Order() {
      }
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getTotal() {
          return total;
      }
  
      public void setTotal(String total) {
          this.total = total;
      }
  
      public String getVat() {
          return vat;
      }
  
      public void setVat(String vat) {
          this.vat = vat;
      }
  
      public String getServiceCharge() {
          return serviceCharge;
      }
  
      public void setServiceCharge(String serviceCharge) {
          this.serviceCharge = serviceCharge;
      }
  
      public String getDeliveryCharge() {
          return deliveryCharge;
      }
  
      public void setDeliveryCharge(String deliveryCharge) {
          this.deliveryCharge = deliveryCharge;
      }
  
      public String getSubtotal() {
          return subtotal;
      }
  
      public void setSubtotal(String subtotal) {
          this.subtotal = subtotal;
      }
  
      public int getPointUsed() {
          return pointUsed;
      }
  
      public void setPointUsed(int pointUsed) {
          this.pointUsed = pointUsed;
      }
  
      public Discount getDiscount() {
          return discount;
      }
  
      public void setDiscount(Discount discount) {
          this.discount = discount;
      }
  
      public User getUser() {
          return user;
      }
  
      public void setUser(User user) {
          this.user = user;
      }
  
      public Address getAddress() {
          return address;
      }
  
      public void setAddress(Address address) {
          this.address = address;
      }
  
      public String getPayementType() {
          return payementType;
      }
  
      public void setPayementType(String payementType) {
          this.payementType = payementType;
      }
  
      public String getPaymentStatus() {
          return paymentStatus;
      }
  
      public void setPaymentStatus(String paymentStatus) {
          this.paymentStatus = paymentStatus;
      }
  
      public String getMessage() {
          return message;
      }
  
      public void setMessage(String message) {
          this.message = message;
      }
  
      public String getTransactionId() {
          return transactionId;
      }
  
      public void setTransactionId(String transactionId) {
          this.transactionId = transactionId;
      }
  
      public String getInvoice() {
          return invoice;
      }
  
      public void setInvoice(String invoice) {
          this.invoice = invoice;
      }
  
      public String getStatus() {
          return status;
      }
  
      public void setStatus(String status) {
          this.status = status;
      }
  
      public String getCreatedAt() {
          return createdAt;
      }
  
      public void setCreatedAt(String createdAt) {
          this.createdAt = createdAt;
      }
  
      public String getUpdatedAt() {
          return updatedAt;
      }
  
      public void setUpdatedAt(String updatedAt) {
          this.updatedAt = updatedAt;
      }
  
  }
  </code></pre>
  
  <p>Order Repository</p>
  
  <pre><code>@Repository
  public interface OrderRepository extends PagingAndSortingRepository&lt;Order, Long&gt; {
  
      List&lt;Order&gt; findByUserAndCreatedAtBetween(User user, String todayString, String sevenDaysBeforeString);
  
  }
  </code></pre>
  
  <p>What am I doing wrong in service while writing method for repository?
  I'm getting a blank array. I think I'm doing something wrong when writing <code>findByUserAndCreatedAtBetween</code>. would be great if could get any suggestion.</p>