2

I am using JDateChooser from here

However I can not retrieve the date in format set with the method setDateFormatString while displaying it in console.

enter image description here

In first label shows actual date retrieved from JDateChooser while second label shows the format which I have set. When I select date from JDateChooser I get the date as 22-07-2011 as shown in image. But when I use getDate method, it will give me date as Fri Jul 22 00:00:00 GMT+05:30 2011. I want only 22-07-2011.

Here is my code. I am using Netbeans IDE 7.0

public JDateChooser() {
    initComponents();
    dateChooser.setDateFormatString("dd-MM-yyyy");
}
private void btnDisplayDateActionPerformed(java.awt.event.ActionEvent evt) {
    String dateFromDateChooser = dateChooser.getDate().toString();
    lblDate.setText(dateFromDateChooser);
    lblDateFormat.setText(dateChooser.getDateFormatString().toString());
    System.out.println(dateFromDateChooser);
}
public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            new JDateChooser().setVisible(true);
        }
    });
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
iOSAppDev
  • 2,755
  • 4
  • 39
  • 77
  • `toString()` always returns the date in the format you see above. You confuse the format you set in the DateChooser (which is the INPUT format) with the format you want to output to the console. You get a `Date` object from the DateChooser which has no internal format. If you want to print it out, you have to format the Date into a String. The answers below show you how. – ZeissS Jul 20 '11 at 11:14

5 Answers5

8

You can use the String.format method to get the desired result.

Date dateFromDateChooser = dateChooser.getDate();
String dateString = String.format("%1$td-%1$tm-%1$tY", dateFromDateChooser);
Kainsin
  • 447
  • 2
  • 8
3

Try this

java.text.SimpleDateFormat fmt = new java.text.SimpleDateFormat("dd-MM-yyyy");
String formattedDate = fmt.format(dateChooser.getDate());
Felype
  • 3,087
  • 2
  • 25
  • 36
2

there are two choises

1/ you already dowloaded code source

    calDealDate.setPreferredSize(new Dimension(90, 23));
    calDealDate.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
    calDealDate.setSpiningCalendarField(Calendar.DAY_OF_MONTH);
    calDealDate.setFont(new Font("SansSerif", Font.BOLD, 12));
    calDealDate.setBackground(AppVariables.fieldColor);
    calDealDate.addChangeListener(new ChangeListener() {

        @Override
        public void stateChanged(ChangeEvent e) {
            //here is your date depends of previous setings
        }
    });
    calDealDate.setToolTipText(" some tooltip text  ");

2/ you already dowloaded jar file, then you have to find this method or equivalent from API

EDIT: maybe I would be preffered to start with code source, just import that into project

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 2
    Thanks for reply.It works. But "Kainsin" solution is better because I just want to print it in defined format & nothing else. Anyways thanks – iOSAppDev Jul 20 '11 at 16:19
  • @iOSAppDev - good point, mKorbel occasionally forgets that answers ideally are as SSCCE as well as questions :-) – kleopatra Apr 17 '13 at 15:10
0

Try This one

   public void calendarExpale(){
    JPanel content = new JPanel();
    content.setLayout(new FlowLayout());
    final JTextField textField = new JTextField(15);
    JDateChooser chooser = new JDateChooser();

   chooser.setSize(15, 10);
  chooser.addPropertyChangeListener("date", new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {

    JDateChooser chooser = (JDateChooser)evt.getSource();
    SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:MM:SS");
    textField.setText(formatter.format(chooser.getDate()));
}
  });

  content.add(textField);
    content.add(chooser);
}
Deepak Singh
  • 460
  • 2
  • 7
  • 19
-1

Use String Format Method, it will help you and how to use it see this

https://www.youtube.com/watch?v=FEM0BpZSXmU

this link has same to same tutorial as you want.