2015-07-12 96 views
1

如何從JCalendar(toedter.com/jcalendar/)獲取JMonthChooser字符串中的月份名稱並將其轉換爲字符串「01」,「02」,「03」, ...,「12」就像使用SimpleDateFormat一樣簡單。如何從JCalendar獲取JMonthChooser上的字符串月份名稱

我會嘗試:

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

但沒有成功

回答

1

我有其他的方式,我需要什麼: 這裏是代碼:

JCalendar jCalendar1 = new JCalendar(); 
String mymonth; 
SimpleDateFormat sdf1 = new SimpleDateFormat("MM"); 
Date date1 = jCalendar1.getDate(); 
mymonth = sdf1.format(date1); 
+0

發生了什麼事'JMonthChooser'? – trashgod

+0

我的主要目標是從JCalendar中找到月份的名稱和格式字符串「01」,「02」,...,「12」中的索引。我很困惑,我想早些時候使用JMonthChooser。但是,然後我可以得到連續的月份和索引名稱使用JCalendar。 我使用的月份的名稱: String mymonth; SimpleDateFormat sdf1 = new SimpleDateFormat(「MMMM」); 日期date1 = jCalendar1.getDate(); mymonth = sdf1.format(date1); Bantuan Anda sangat saya apresiasi。 Terima kasih。 – repot

+0

你想要的Locale不支持? – trashgod

4

鑑於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)); 
    } 
});