0

I have a List of Travels in my db. I just want to show the travels with not expired date in frontend. I have tried findByTravelDateAfter, but it show me only the date after the date, that I gave as Input. How can I do if we are for example 2021-10-12 and I give as Input 2021-10-22 and I want to display all the travels befor and after 2021-10-22 but only after 2021-10-12.

Can someone give me Idea how to do this ?

Here is my entity:

import lombok.Getter;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.sql.Date;
@Getter
@Setter
@Entity
public class ProposeTravel {
    @Id
    @GeneratedValue
    public Long travelId;
    public String startCity;
    public String endCity;
    public Date travelDate;
    public String travelTime;
    public Long numberOfPlace;
    public double travelPrice;
    public String startPoint;
    public String carDetails;
    public String baggageDetails;
    @Column(nullable = false)
    public String emailDriver;
    @NotBlank
    @NotNull
    public String drivingLicenceNr;
    public Long userId;
    public ProposeTravel() {
        super();
        // TODO Auto-generated constructor stub
    }
    public ProposeTravel(Long travelId, String startCity, String endCity, Date travelDate, String travelTime,
            Long numberOfPlace, double travelPrice, String startPoint, String carDetails, String baggageDetails,
            String emailDriver, Long userId) {
        super();
        this.travelId = travelId;
        this.startCity = startCity;
        this.endCity = endCity;
        this.travelDate = travelDate;
        this.travelTime = travelTime;
        this.numberOfPlace = numberOfPlace;
        this.travelPrice = travelPrice;
        this.startPoint = startPoint;
        this.carDetails = carDetails;
        this.baggageDetails = baggageDetails;
        this.emailDriver = emailDriver;
        this.userId = userId;
    }
}

My repository:

import cm.camtougo.apicamtougo.TravelModule.entities.ProposeTravel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;


@Repository
public interface ProposeTravelRepository extends JpaRepository<ProposeTravel, Long> {

    ProposeTravel findByTravelId(Long travelId);
    public List<ProposeTravel> findByUserId(Long userId);
    List<ProposeTravel> findByStartCityAndTravelDateAfterAndEndCity(String startCity, Date travelDate, String endCity);
    List<ProposeTravel> findByStartCityAndTravelDateAndEndCity(String startCity, Date travelDate, String endCity);
    List<ProposeTravel> findByStartCityAndTravelDateAfter(String startCity, Date travelDate);
    List<ProposeTravel> findByEndCityAndTravelDateAfter(String endCity, Date travelDate);
    List<ProposeTravel> findByStartCityAndEndCityAndTravelDateAfter(String startCity, String endCity, Date travelDate);
}
Valero
  • 11
  • 1
  • Hi can you please add some of your code? especially the Entity class that you want to query and/or your repository where you wrote the `findByTravelDateAfter` method – pleft Oct 12 '21 at 07:45
  • This will help you https://stackoverflow.com/questions/39784344/check-date-between-two-other-dates-spring-data-jpa @Valero – JCompetence Oct 12 '21 at 07:46
  • you can do it multiple ways, one is to set a condition. please add your code so we can help you in a better way. – Issa Khodadadi Oct 12 '21 at 07:48
  • It wasn't me that downvoted, however this question needs more input from @Valero especially to share his code. – pleft Oct 12 '21 at 07:48
  • "all the travels befor and after 2021-10-22" - wouldn't that mean "all travels except those at that date"? And if you include the given date, this would then just be "all travels". The addition "but only after 2021-10-12" would make it effectively "date > 2021-10-12". – Thomas Oct 12 '21 at 08:03
  • Another question: "show the travels with not expired date in frontend" and "show me only the date after the date, that I gave as Input." - does that mean that you want to return all travels that have a date after the input date _or_ today - whichever is later? If so I'd make that selection before passing the resulting date as a parameter to the query. Also, do you mean _after_ (as in "x > date") or _not before_ (as in "! x < date" or "x >= date")? – Thomas Oct 12 '21 at 08:07
  • For example: actualDate is 2021-10-12, I give as Input 2021-10-22 and I would like to result all travels 5 Days befor 2021-10-22 and after 2021-10-22. If the differenz between actualDate and InputDate is smaller than 5, I don't like to display result befor actualDate – Valero Oct 12 '21 at 10:19

0 Answers0