如何从JMonthChooser(toedter.com/jcalendar/)在JCalendar的字符串中获取月份名称,并将其转换为字符串“ 01”,“ 02”,“ 03”,...,“ 12”使用SimpleDateFormat

我会尽力 :

String mymonth;
SimpleDateFormat sdfm = new SimpleDateFormat("MM");
JComboBox combom = (JComboBox)jMonthChooser1.getSpinner();
mymonth = sdfm.format(((JTextField)combom.getEditor()).getText());


但是没有成功

最佳答案

给定JMonthChooser的实例,PropertyChangeListener将看到Integer范围内的0 .. 11类型的新值。与其尝试将其强制为适用于SimpleDateFormat的日期,不如考虑使用适当的Formatter

JMonthChooser jmc = new JMonthChooser();
jmc.addPropertyChangeListener("month", new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        System.out.println(e.getPropertyName() + ": "
            + String.format("%02d", ((Integer) e.getNewValue()).intValue() + 1));
    }
});

07-24 14:20