coder123
coder123

Reputation: 25

setting minimum selectable date as 15 years past birthday (from mySQL) and max as current date

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

Upvotes: -1

Views: 1274

Answers (2)

Anonymous
Anonymous

Reputation: 86389

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.

Upvotes: 0

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

From java doc there are setSelectableDateRange, which can accept two dates, min and max :

public void setSelectableDateRange(java.util.Date min, java.util.Date max)

In your case you need something like this :

Calendar cal = Calendar.getInstance();
cal.add(Calendar.YEAR, -15);//15 year before
Date min = cal.getTime();

Date max = new Date();//actual date

JDateChooser jDateChooser = new JDateChooser();
//set your dates in your JDateChooser
jDateChooser.setSelectableDateRange(min, max);

Upvotes: 0

Related Questions