tl;dr
if( file_getstartdate<= currentDate<= file_getendDate) { … }
LocalDateRange // Represent a span-of-time as a pair of `LocalDate` (date-only) objects.
.ofClosed( startLocalDate , stopLocalDate ) // Making the date range in fully-closed approach, against my advice of using half-open approach.
.contains( // Compares a specified `LocalDate` against the start and stop dates of the range.
LocalDate.now( // Use `java.time.LocalDate` to represent a date-only value without a time-of-day and without a time zone.
ZoneId.of( "Africa/Tunis" ) // Specify the time zone by which we want to perceive the calendar date for the current moment. "Tomorrow" arrives in Paris France while still "yesterday" in Montréal Québec.
) // Returns a `LocalDate` object.
) // Returns a `boolean` primitive.
java.time
The Answer by Player One is correct, but can be improved by using the modern java.time classes rather than the terrible legacy classes (Date
, etc.).
LocalDate
For a column of type SQL-standard DATE
, use the LocalDate
class. The LocalDate
class represents a date-only value without time-of-day and without time zone or offset-from-UTC.
Time zone
A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument. If critical, confirm the zone with your user.
ZoneID
Specify a proper time zone name in the format of Continent/Region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 2-4 letter abbreviation such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
LocalDate today = LocalDate.now( z ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Half-Open
if( file_getstartdate<= currentDate<= file_getendDate){
You will find your work easier if you consistently use the Half-Open approach to defining a span-of-time. In this approach, the beginning is inclusive while the ending is exclusive. So a month starts on the first and runs up to, but does not include, the first of the following month.
So your query logic would be, like the following, with <=
& <
.
if( file_getstartdate <= currentDate < file_getendDate)
This means in SQL, do not use BETWEEN
for date-time work.
String sql = "SELECT * from tbl WHERE when >= ? AND when < ? ;" ;
…
myPreparedStatement.setObject( 1 , today ) ;
myPreparedStatement.setObject( 2 , today ) ;
To retrieve DATE
, values:
LocalDate localDate = myResultSet.getObject( … , LocalDate.class ) ;
The same kind of half-open logic within Java code would look like the following code. Note that a shorter way of saying "is equal to or is later than" is "is not before".
boolean isTodayWithinDateRange =
( ! today.isBefore( startDate ) ) // Is today same or later than start…
&& // AND
today.isBefore( stopDate ) // Is today before the stop (for Half-Open approach).
;
Do not conflate date-time values with text
My date stored in my table is in the format YYYY-MM-DD
No it is not. The DATE
type in MySQL stores a date by its own internally-defined mechanism, not plain text.
A date-time object such as DATE
column in database or LocalDate
in Java can parse a string input into a date value, and can generate a string from that date value. But the date value is not itself string. Do not confuse the two. In other words, a date value does not have “format”, only their textual representations have a format.
LocalDateRange
If doing much of this work, add the ThreeTen-Extra library to your project. This gives you access to the LocalDateRange
class.
LocalDate start = … ;
LocalDate stop = … ;
LocalDateRange range = LocalDateRange.of( start , stop ) ;
boolean rangeContainsToday = range.contains( today ) ;
By default the LocalDateRange
class works using Half-Open approach. But if you insist on your full-closed approach, override its default behavior with LocalDateRange.ofClosed
method.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.