'How to get month name in string on JMonthChooser from JCalendar

How to get month name in string on JMonthChooser from JCalendar (toedter.com/jcalendar/) and convert it to string "01", "02","03",...,"12" as simple as using SimpleDateFormat.

I'll try :

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

But no success



Solution 1:[1]

Given an instance of JMonthChooser, a PropertyChangeListener will see a new value of type Integer in the range 0 .. 11. Rather than trying to coerce this to a date suitable for SimpleDateFormat, consider using a suitable 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));
    }
});

Solution 2:[2]

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";
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trashgod
Solution 2 Dinesh Tharaka