1

I am creating an employee parole system which include accepting the date of joining.

I am using swings to create an interface in java. I want the user to set the value of spinner in the date and the program must be able to obtain the day month and year selected by the user.

My employee object consists of a variable of class Date which is created by me.

I want an object of employee to be created when the user clicks the submit button.

Im unable to find the solution.

Here are a few snippets of my program.

mainframe.java

private JSpinner sdoj;
private SpinnerDateModel sp;
sp=new SpinnerDateModel();

sdoj=new JSpinner(sp);
submit.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
    Employee emp=new Employee();

    emp.setDOJ(sp.getCalendarField()); //this is something i have tried but i am not successful
     }
}

Employee.java

public class Employee {

    private int employeeId;
    private String employeeName,employeeAddress;
    private boolean bC, bCPlus,bJava;
    private EnumGender eGender;
    private EnumDepartment eDepartment;
    private EnumQualification eQualification;
    private Date DOJ;

    public Employee() {
       // TODO Auto-generated constructor stub
    }
}

Date.java

public class Date {
    private int day,month,year;

    public Date(int day, int month, int year) {
        super();
        this.day = day;
        this.month = month;
        this.year = year;
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
one.whoknocks
  • 58
  • 1
  • 7

1 Answers1

2

I think you want:

@Override
public void actionPerformed(ActionEvent arg0) {
    Employee emp=new Employee();
    emp.setDOJ(sp.getDate());//changed to getDate as setDOJ accepts Date parameter
}

SpinnerDateModel#getDate()

As per docs:

Returns the current element in this sequence of Dates. This method is equivalent to (Date)getValue.

NB the Date object returned does not refer to your own custom Date class but rather the java.util.Date

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • your method works but it consists of the time too. is there any wa to get only the date and not the time – one.whoknocks Jul 31 '13 at 19:20
  • @user2625973 It returns a `Date` object which has methods to get the year, month and day separately as well as minutes, hours and seconds. See the last link in answer on `java.util.Date`. Also have a look at the link in mKorbels answers which uses [`SimpleDateFormat`](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) when outputting only the date and not the time – David Kroukamp Jul 31 '13 at 19:23