tl;dr
SQL…
SELECT * FROM person WHERE birthdate <= ? ;
For years…
LocalDate.now( ZoneId.of( "Africa/Tunis" ) )
.minus( Period.ofYears( 15 ) )
And for months…
LocalDate.now( ZoneId.of( "Africa/Tunis" ) )
.minus( Period.ofMonths( 6 ) )
Details
The accepted Answer by GreyBeardedGeek is correct. You should be storing a date value in a date type of column. Frame your queries as finding rows whose birthdate is before/after a certain date for a certain age.
java.time
Here is some Java code as example. The modern approach uses the java.time classes built into Java 8 and later.
Avoid the troublesome old date-time classes such as Date
and Calendar
. They are poorly-designed and confusing. They are now legacy as of JSR 310.
LocalDate
The LocalDate
class represents a date-only value without time-of-day and without 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.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-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 );
Period
Represent a span of time unattached to the timeline as a Period
or Duration
.
Period fifteenYears = Period.ofYears( 15 ) ;
Calculate the target date for the SQL query.
LocalDate ld = today.minus( fifteenYears ) ; // Get the date fifteen years ago.
Your SQL will run something like this example. The ?
is a placeholder for the LocalDate
value determined above. Whether to compare using <=
or <
depends on your business rules’ definition of age.
SELECT *
FROM person
WHERE birthdate <= ?
;
Your issue about months versus years is no problem at all. Use a Period
of months rather than years.
Period sixMonths = Period.ofMonths( 6 ) ;
You may want to make that range of likely Period
objects to be constants or perhaps an Enum
.
JDBC 4.2
As of JDBC 4.2, we can directly exchange java.time types with a database. The date-time related java.sql classes are now legacy, such as java.sql.Timestamp
and java.sql.Date
. Good riddance to those kludges.
myPreparedStatement.setObject( 1 , ld ) ;
…and…
LocalDate birthdate = myResultSet.getObject( … , LocalDate.class ) ;
Two criteria
If you want only 15-year-olds, for example, and not 16-year-olds, use two criteria, a pair of earlier and later birthdates.
LocalDate earlierBirthDate = today.minus( Period.ofYears( 16 ) ) ;
LocalDate laterBirthDate = today.minus( Period.ofYears( 15 ) ) ;
The SQL would be something like this:
SELECT *
FROM person
WHERE birthdate > ?
AND birthdate <= ?
;
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
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
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.