Spring Boot
Spring Boot
Creating a Spring Boot Project
Making HTTP GET Requests with Query Parameters in Spring Boot
Making HTTP POST Requests in Spring Boot
Calling a REST API in Spring Boot
Validating Request Input in Spring Boot
Uploading Files in Spring Boot
Downloading Files in Spring Boot
Upload Image File as Base64 String in Spring Boot
How to Zip Multiple Files for Download in Spring Boot/Java
Spring Boot with MySQL
Spring Boot with MongoDB
Spring Data JPA Query Methods
Pagination and Sorting in Spring Boot
Enable Global CORS in Spring Boot
How to Write Unit Tests in Spring Boot - Best Practices and Examples
Convert XML to JSON in Spring Boot
Convert JSON to XML in Spring Boot
Change Logback Log Level Programmatically at Runtime via a REST API in Spring Boot
Exporting Data from MySQL Database to Excel in Spring Boot via REST API using Apache POI Library
Importing Excel Data into MySQL Database in Spring Boot via REST API using Apache POI Library
JWT Authentication using Spring Security OAuth2 in Spring Boot Example
Spring JPA Query to Check if the Current Date is Between a Range of Two Dates
Spring JPA Query to Find the Offered Price on the Current Date by Searching Between a Range of Two Dates
MongoDB Query to Find Ranking of Records in Spring Boot Java
Using Multiple Databases with Spring Data JPA in Spring Boot
Convert HTML to PDF for download in Spring Boot
Building Microservices using Spring Boot in 2024
Calling a Rest API using WebClient in Spring Boot
Spring JPA Query to Find the Offered Price on the Current Date by Searching Between a Range of Two Dates
- Last updated Apr 25, 2024
To find the price based on the current date by searching between a "from" and "to" date, you can do the following:
Suppose you have an entity named ProductPrice with fields fromDate, toDate, and value, representing the price over a specific period:
import java.util.Date;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity
@Table(name = "product_price")
public class ProductPrice {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String id;
@Column(name = "price")
private double price;
@Column(name = "from_date")
private Date fromDate;
@Column(name = "to_date")
private Date toDate;
}
Query method to find the offered price on the current date by searching between a range of two dates:
import java.util.Optional;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.price.entity.ProductPrice;
@Repository
public interface ProductPriceRepository extends CrudRepository<ProductPrice, Long> {
@Query("SELECT pp.price FROM ProductPrice pp WHERE pp.fromDate <= CURRENT_DATE AND pp.toDate >= CURRENT_DATE")
Optional<Double> findOfferedPriceForCurrentDate();
}
Here's another example of a JPA query method to find the offered price between the start and the end date based on the current date:
import java.util.Date;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.example.price.entity.ProductPrice;
@Repository
public interface ProductPriceRepository extends CrudRepository<ProductPrice, Long> {
Optional<ProductPrice> findByFromDateLessThanEqualAndToDateGreaterThanEqual(Date currentDate1,
Date currentDate2);
}
Now, in your service class, you can use the repository method to find the appropriate price object:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;
import java.util.TimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.price.entity.ProductPrice;
import com.example.price.repository.ProductPriceRepository;
import com.example.price.service.PriceService;
@Service
public class PriceServiceImpl implements PriceService {
@Autowired
private ProductPriceRepository productPriceRepository;
@Override
public Double getOfferedPrice() {
Date serverCurrentDate = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
// setting time zone of your database
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
String currentDateStr = dateFormat.format(serverCurrentDate);
Date currentDate = null;
try {
currentDate = dateFormat.parse(currentDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
Optional<ProductPrice> productPriceOpt =
productPriceRepository.findByFromDateLessThanEqualAndToDateGreaterThanEqual(currentDate, currentDate);
if (productPriceOpt.isPresent()) {
// return discount price
return productPriceOpt.get().getPrice();
}
// should return original price
return null;
}
}