Reputation: 13
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?
Upvotes: 0
Views: 3221
Reputation: 15
String dob =new SimpleDateFormat("dd-MMM-yyyy").format(jDateChooser1.getDate());
Upvotes: 0
Reputation: 12204
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
Upvotes: 0
Reputation: 347244
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
Upvotes: 0