开发者

Unwanted data using JDateChooser in JCalendar

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). 开发者_Python百科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);   


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());
                }
            }
        });


I'm use code source, not compiled file in the *.jar, then

calNewDate.setDateFormat(new SimpleDateFormat("dd.MM.yyyy"));
calNewDate.setSpiningCalendarField(Calendar.DAY_OF_MONTH);
calNewDate.setFont(new Font("SansSerif", Font.BOLD, 12));
calNewDate.setBackground(someColor);
calNewDate.addChangeListener(new ChangeListener() {

    @Override
    public void stateChanged(ChangeEvent e) {
       //some stuff
    }
});
calNewDate.setToolTipText("Whatever");


Please, make attention in formatting the stamp of the date in the JDateChososer textField. A common lexical mistake you can do is the following I supposed a addPropertyChangeListener behind the JDateChooser to catch the date user sets in input:

dateInserted.getDateEditor().addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                // TODO Auto-generated method stub
                if ("date".equals(evt.getPropertyName())) {
                    Date date = dateInserted.getDateEditor().getDate();
                    ordine.setOrderDate(date);
                    dateInserted.setDateFormatString("dd/MM/YYYY");
                    dateInserted.setDate(date);
                    System.out.println(date.toString());
                    dateInserted.setBorder(BorderFactory.createLineBorder(Color.GREEN));
if (canIenableCalcolaEAggiungi(2) == true)
                        calculatingAndAdding.setEnabled(true);
                    else {
                        calculatingAndAdding.setEnabled(false);
                    }
                }
            }
        });

In the script it has been wrongly written:

 dateInserted.setDateFormatString("dd/MM/YYYY");

When you choose the year's format pattern "YYYY" (instead of "yyyy" or "yy" cfr API) it is not recognized and generates a failure as follows: when you try editing the JDateChooser's textfield and you make a mouse step-off, the date automatically changed in a random value. It can't be changed anymore. That could generate an unwanted date as well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜