如何从 JCalendar 获取 JMonthChooser 字符串中的月份名称

How to get month name in string on JMonthChooser from JCalendar

如何从 JCalendar (toedter.com/jcalendar/) 获取 JMonthChooser 字符串中的月份名称并将其转换为字符串“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 将在 0 .. 11 范围内看到 Integer 类型的新值。与其试图将其强制为适合 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));
    }
});

我得到了我需要的另一种方式: 这是代码:

JCalendar jCalendar1 = new JCalendar();
String mymonth;
SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
Date date1 = jCalendar1.getDate();
mymonth = sdf1.format(date1);
String month;
if(jMonthChooser1.getMonth()==0){
    month = "Jan";
}else if(jMonthChooser1.getMonth()==1){
    month = "Feb";
}else if(jMonthChooser1.getMonth()==2){
    month = "Mar";
}else if(jMonthChooser1.getMonth()==3){
    month = "Apr";
}else if(jMonthChooser1.getMonth()==4){
    month = "May";
}else if(jMonthChooser1.getMonth()==5){
    month = "Jun";
}else if(jMonthChooser1.getMonth()==6){
    month = "Jul";
}else if(jMonthChooser1.getMonth()==7){
    month = "Aug";
}else if(jMonthChooser1.getMonth()==8){
    month = "Sep";
}else if(jMonthChooser1.getMonth()==9){
    month = "Oct";
}else if(jMonthChooser1.getMonth()==10){
    month = "Nov";
}else{
    month = "Dec";
}