MsLaus
MsLaus

Reputation: 55

problems with JDateChooser

I just started my first complex java program, which is a bank system program. I want to check if the user is an adult or not. I added a JDateChooser to the frame, i can choose a date, but my problem is that i can not take only the year.

`import com.toedter.calendar.JDateChooser;
 import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import java.util.*;

 JDateChooser dateChooser = new JDateChooser();
 public boolean isAdult() {

            Date date = new Date();
            int currentYear = date.getYear();
            int clientYear;

            if (dateChooser.toString().length() > 4) {
                clientYear =         Integer.parseInt(dateChooser.toString().substring(dateChooser.toString().length() - 4));
                System.out.println(clientYear);
            } else {
                clientYear = Integer.parseInt(date.toString());
            }
            int age = currentYear - clientYear;
            if (age < 18) {
                return false;
            } else return true;
        }``

Upvotes: 0

Views: 66

Answers (1)

kretob
kretob

Reputation: 191

I suppose you want to read just the year from the JDateChooser.

To do this easily, use the getDate() method of the JDateChooser. It will return an instance of java.util.Date. You can then use the method getYear() to get the year of type int

Upvotes: 0

Related Questions