I have a mySQL table of users containing the users' birthdays.
i have stored the date from sql as the variable DateOfBirth.
Now, I want to set the minimum selectable date of a jDateChooser named GameDateChooser as 15 years past the birthday
and the maximum selectable date to the current date.
i tried searching for other articles but they didnt answer my question
setting minimum selectable date as 15 years past birthday (from mySQL) and max as current date
1.2k Views Asked by coder123 At
2
There are 2 best solutions below
0
On
Even when setSelectableDateRange() requires two oldfashioned Date objects, I would still prefer to use the modern classes for the age calculation, not the outdated Calendar:
LocalDate dateOfBirth = LocalDate.of(1959, Month.FEBRUARY, 15);
Instant min = dateOfBirth.plusYears(15)
.atStartOfDay(ZoneId.systemDefault())
.toInstant();
jDateChooser.setSelectableDateRange(Date.from(min), new Date());
LocalDate and Instant come built-in with Java 8 and later and are also available for Java 6 and 7. Date.from(Instant) is Java 8 and later only, for Java 6 and 7 there are other conversion options. A sufficiently new MySQL JDBC driver should be able to retrieve a LocalDate from the database.
From java doc there are setSelectableDateRange, which can accept two dates, min and max :
In your case you need something like this :