3

I have a situation that I need to fill the jDateChooser box by the current date automatically (without clicking the pop up calender).

How can I do that?

Thanks in advance.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
Erfan Ahmed
  • 1,536
  • 4
  • 19
  • 34
  • [This one](http://www.se.rit.edu/~sal/uitutorials/uitot/api/com/toedter/calendar/JDateChooser.html#setDate(java.util.Date))? – Steffen Apr 23 '15 at 21:02
  • I found my answer. Sorry if i bother anyone guys! – Erfan Ahmed Apr 23 '15 at 21:09
  • I found this one easier -- first i need to pick up the current date DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); Date date = new Date(); then just set it to the jdatechooser field jDateChooser.setDate(date); – Erfan Ahmed Apr 23 '15 at 21:13
  • Why do you need the format? – MadProgrammer Apr 23 '15 at 21:30
  • aah, actually I am working with mysql. There I need date in this format yyyy-MM-dd to save it or edit the existing data. that's why I formatted it but for simple use you don't need to format date. :) – Erfan Ahmed Apr 23 '15 at 21:34

3 Answers3

4
Date date = new Date();
dateChooser.setDate(date);
  • 2
    While this post may answer the question, it is still a good idea to add some explanation and, possibly, some links to the relevant documentation. Answers with good explanations and references are usually more useful both to the current OP and to the future visitors. Full detailed answers are also more likely to attract positive votes. – Eugene Podskal May 25 '15 at 20:20
0

Here, the Date class belongs to the java.util package. So, either import java.util.Date or use:

java.util.Date date = new java.util.Date();
dateChooser.setDate(date);
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
0

To pick up the current date with desired format (format is necessary when you work with database) -

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
Date date = new Date(); 

Then just set it to the jdatechooser field

jDateChooser.setDate(date);

Erfan Ahmed
  • 1,536
  • 4
  • 19
  • 34