-1

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

coder123
  • 25
  • 9
  • 1
    See http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry May 04 '17 at 08:32

2 Answers2

0

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);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
0

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.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161