简体   繁体   中英

How to retrieve chosen date from JDatechooser?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM