Unwanted data using JDateChooser in JCalendar

Paul Kar. picture Paul Kar. · Oct 3, 2011 · Viewed 7.8k times · Source

I am using JDateChooser in JCalendar (with Swing). I am trying to get a format of "yyyy-MM-dd", but for some reason I get the time also and it's always the same(00:00:00 MDT). Anyone has an idea how to get rid of the time? Thanks in advance.

try {
    calendarDate = new JDateChooser();
} catch (Exception e) {
    e.printStackTrace();
}
calendarDate.setDateFormatString("yyyy-MM-dd");

dateLabel = new JLabel("Date");
parent.frame2.getContentPane().add(dateLabel);//1
parent.frame2.getContentPane().add(calendarDate);   

Answer

ring bearer picture ring bearer · Oct 3, 2011

To get JDateChooser to show a specific date format, you need to set that specific format using its setDateFormatString API

Example :

JDateChooser myDateChooser = new JDateChooser();
myDateChooser.setDateFormatString("yyyy-MM-dd");

Are you already doing gthis ? Then you must post the place where you are getting value from the component.

And in the place where you handle property change of JDateChooser, you may do something like the following to get the date in same format :

Example: (assuming String dateString is where you want the date string)

myDateChooser.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("date")) {
                    dateString = new SimpleDateFormat("yyyy-MM-dd").format(myDateChooser.getDate());
                }
            }
        });