0

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.

Abra
  • 11,631
  • 5
  • 25
  • 33
  • 2
    *"What can I do?"* For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). It should only take 20-30 lines of code to reproduce the compiler error. – Andrew Thompson Feb 26 '21 at 14:37
  • Note that the format string `YYYY-MM-DD` is most likely wrong. Read the [API docs](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/SimpleDateFormat.html) - you probably need `yyyy-MM-dd`. Case is important, when you use a different case it means a different thing. (This is however not the cause of the error you are asking about). – Jesper Feb 26 '21 at 14:50
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. Instead use `LocalDate` and `DateTimeFormatter`, both from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). You may also want to go looking for a date picker that support java.time if the one you are using now doesn’t. – Ole V.V. Feb 26 '21 at 19:13
  • Does this answer your question? [Java Date() giving the wrong date \[duplicate\]](https://stackoverflow.com/questions/14836004/java-date-giving-the-wrong-date) – Ole V.V. Feb 26 '21 at 19:14

1 Answers1

1

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.

Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
Abra
  • 11,631
  • 5
  • 25
  • 33