TL; DR
SQL ...
SELECT * FROM person WHERE birthdate <= ? ;
For many years...
LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) .minus( Period.ofYears( 15 ) )
And for several months ...
LocalDate.now( ZoneId.of( "Africa/Tunis" ) ) .minus( Period.ofMonths( 6 ) )
More details
The accepted answer of GreyBeardedGeek is correct. You must store the date value in the column date type. Create your queries as a search for strings whose birth date is before / after a certain date for a certain age.
java.time
Here is an example of Java code. The modern approach uses the java.time classes built into Java 8 and later.
Avoid the nasty old time classes like Date
and Calendar
. They are poorly designed and confused. Now they are deprecated from JSR 310 .
LocalDate
The LocalDate
class represents a date value only without time and without a time zone.
The time zone is critical for determining the date. At any given moment, the date changes around the world by zone. For example, a few minutes after midnight in Paris, France is a new day, still "yesterday" in Montreal Quebec .
Specify the time zone name in continent/region
format, such as America/Montreal
, Africa/Casablanca
or Pacific/Auckland
. Never use the abbreviation 3-4 letters, for example EST
or IST
, as they are not real time zones, and are not standardized and not even unique (!).
ZoneId z = ZoneId.of( "America/Montreal" ); LocalDate today = LocalDate.now( z );
Period
Imagine a time span that is not tied to a timeline, like Period
or Duration
.
Period fifteenYears = Period.ofYears( 15 ) ;
Calculate the target date for the SQL query.
LocalDate ld = today.minus( fifteenYears ) ;
Your SQL will run something like this example. ?
is a placeholder for the LocalDate
value defined above. A comparison using <=
or <
depends on your age-specific business rules.
SELECT * FROM person WHERE birthdate <= ? ;
Your problem about months compared to years is not a problem at all. Use Period
months, not years.
Period sixMonths = Period.ofMonths( 6 ) ;
You might want to make this range of probable Period
objects permanent, or perhaps Enum
.
JDBC 4.2
As in JDBC 4.2, we can directly exchange java.time types with the database. Time-related java.sql classes are now deprecated, for example java.sql.Timestamp
and java.sql.Date
. Good disposal of these bugs.
myPreparedStatement.setObject( 1 , ld ) ;
... and ...
LocalDate birthdate = myResultSet.getObject( … , LocalDate.class ) ;
Two criteria
If you need only 15-year-olds, for example, rather than 16-year-olds, use two criteria: a pair of early and late birth dates.
LocalDate earlierBirthDate = today.minus( Period.ofYears( 16 ) ) ; LocalDate laterBirthDate = today.minus( Period.ofYears( 15 ) ) ;
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 supersede the nasty old legacy datetime classes such as java.util.Date
, Calendar
and SimpleDateFormat
.
The Joda-Time project, now in maintenance mode , is advised to switch to the java.time classes.
To learn more, see the Oracle Tutorial . And search for qaru for many examples and explanations. JSR 310 specification .
Where to get java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is proof of possible future additions to java.time. Here you can find useful classes such as Interval
, YearWeek
, YearQuarter
and more .