0

I am using Eclipse to do a college project on Java. I realized that java does not have a built in date selector like C#, so I downloaded and added JDateChooser. I tried to retrieve the chosen date but it failed:

 String Date = dateChooser.getDate(); //I want to the date to be retrieved as string

Any ideas? Is there some kind of initialization that I must do?

Mohsen Kamrani
  • 7,177
  • 5
  • 42
  • 66
XenonDragon
  • 13
  • 2
  • 9

3 Answers3

0

If I have the right component, the Java Docs show that the getDate method returns Date

getDate

public java.util.Date getDate()

Returns the date. If the JDateChooser is started with a null date and no date was set by the user, null is returned.

Returns:
the current date

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Retrieve the date that the user selected by calling getDate(), which returns a Date object. Then convert that object into a String by calling SimpleDateFormat.format():

Date              d;
SimpleDateFormat  sdf;
String            s;

d = dateChooser.getDate();                      // Date selected by user
sdf = SimpleDateFormat("MM/dd/yyyy HH:mm:ss");  // Or whatever format you need
s = sdf.format(d);                              // Viola

See also: Question 5683728

Community
  • 1
  • 1
David R Tribble
  • 11,918
  • 5
  • 42
  • 52
  • Hello, and thanks a lot. Everytime I try that, I get an error on dateChooser.getDate(). It says that "dateChooser cannot be resolved" although I am sure that the name in the design is "dateChooser" – XenonDragon Apr 08 '14 at 21:15
  • I also made sure that it is added to the build path, but I still have the error and cannot run it. Please help me. What are the things that I should do before I am able to use datechooser? I might be missing something – XenonDragon Apr 08 '14 at 21:40
0

String dob =new SimpleDateFormat("dd-MMM-yyyy").format(jDateChooser1.getDate());

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 15 '22 at 05:50
  • Today no one needs `SimpleDateFormat` nor `Date`. We should all be using [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). Also does your answer provide anything substantial that isn’t already in the accepted answer? – Ole V.V. Apr 15 '22 at 07:27