1

如何JMonthChooserJCalendar(toedter.com/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());

但没有成功

4

3 回答 3

4

给定一个 的实例JMonthChooser, a将在范围内PropertyChangeListener看到一个新的 type 值。与其试图强迫它选择一个合适的日期,不如考虑使用一个合适的.Integer0 .. 11SimpleDateFormatFormatter

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));
    }
});
于 2015-07-12T05:10:04.187 回答
1

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

JCalendar jCalendar1 = new JCalendar();
String mymonth;
SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
Date date1 = jCalendar1.getDate();
mymonth = sdf1.format(date1);
于 2015-07-16T01:26:12.093 回答
0
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";
}
于 2022-01-30T20:58:29.727 回答