Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top