0

I have a database of suppliers, they rent car. Most people rent for a range of 2-3 days. on the database i have the rent start date/time stored in as datetime format. the rent end date is also in date time format.

suppose someone wants to rent the same car, if the requested range of date falls in one of the ranges of date the car is already rented out - how can i check the availability? i am using beans, servlets and jstl on the jsp pages.

i guess it should be a comparator?

1 Answer 1

4

If you're doing the comparisons in Java, the java.util.Date class already implements Comparable so you can just use its compareTo() method. But you can probably also do the comparisons in your database, using the < and > operators in SQL.

There's also the SQL OVERLAPS function, which lets you compare two date ranges to see if they overlap each other. That's probably simpler and more efficient, if your database supports it.

For example, supposing your table is called "reservations", you could do:

SELECT id FROM reservations
WHERE (DATE '2012-03-29', DATE '2010-04-01') OVERLAPS (start_date, stop_date)
  AND car_id = 12345;

And if that returns any rows, it means there's already a reservation for car 12345 that conflicts with that date range.

Sign up to request clarification or add additional context in comments.

2 Comments

could you provide me a link to any good resource on the compareto() method
How about Oracle's documentation for the Date class? You should have that site bookmarked already if you're doing Java development.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.