Reputation: 74
I'm trying to get the date from DateChooserCombo
as follows
SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD");
String date = sdf.format(dateChooser.getDate());
But the method getDate()
gives me error (illegal forward reference). I have also tried with getSelectedDate()
but it's the same. What can I do?
Anyway I'm using Apache Netbeans 12.1 and the date picker should be this one: https://github.com/vadimig/jdatechooser
Thanks.
Upvotes: 0
Views: 792
Reputation: 20914
I downloaded the JDateChooser
code from the link you provided in your question. There is no getDate()
method in class datechooser.beans.DateChooserCombo
. There is a getSelectedDate()
method which returns an instance of class java.util.Calendar.
Also, according to the documentation for class java.text.SimpleDateFormat
, the pattern YYYY-MM-DD
is a valid pattern but I don't think it's the pattern that you want. D
means day in year which means that 27th February is the 58th day of the year. You probably want d
. Similarly, Y
means Week year
whereas you probably wanted y
.
So, in order to get a string representation of the date that the user selected from the DateChooserCombo
, you probably want the following code.
DateChooserCombo dcc = new DateChooserCombo(); // or however you create and configure it
Calendar cal = dcc.getSelectedDate();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(cal.getTime());
By the way, it appears that JDateChooser
development stopped seven years ago. Perhaps consider using JavaFX which has a DatePicker component which works with Java's date-time API.
Upvotes: 1