1

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

repot
  • 83
  • 3
  • 9

3 Answers3

4

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));
    }
});
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I got the other way that what I need : Here are the code :

JCalendar jCalendar1 = new JCalendar();
String mymonth;
SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
Date date1 = jCalendar1.getDate();
mymonth = sdf1.format(date1);
repot
  • 83
  • 3
  • 9
  • What happened to `JMonthChooser`? – trashgod Jul 16 '15 at 10:26
  • My main goal is to find the name of the month from JCalendar and indexes in the format string "01", "02", ..., "12". I was confused, I guess using JMonthChooser earlier. But then I could get straight month and the index names used JCalendar. The name of the month I get using: String mymonth; SimpleDateFormat sdf1 = new SimpleDateFormat("MMMM"); Date date1 = jCalendar1.getDate(); mymonth = sdf1.format(date1); Bantuan Anda sangat saya apresiasi. Terima kasih. – repot Jul 17 '15 at 01:27
  • Is your desired `Locale` not supported? – trashgod Jul 17 '15 at 01:33
  • 1
    Bantuan Anda sangat saya apresiasi. Terima kasih. (I really appreciated your help @trashgod. Thank you.) – repot Jul 17 '15 at 01:38
  • 1
    It appears to be supported [here](http://www.oracle.com/technetwork/java/javase/java8locales-2095355.html). – trashgod Jul 17 '15 at 01:39
  • Thanks @trashgod. I'm using toedter.com/jcalendar for Netbeans, I'm still didn't find my Locale in JCalendar properties on Netbeans. Can you help me? – repot Jul 17 '15 at 01:55
  • 1
    Try `new SimpleDateFormat("MMMM", new Locale("in", "ID"))`. – trashgod Jul 17 '15 at 02:35
  • Many Thanks @trashgod – repot Jul 17 '15 at 02:39
  • I use `jCalendar1.setLocale(Locale.forLanguageTag("ID"));` in my `jCalendar1PropertyChange` for Indonesian `Locale` – repot Jul 17 '15 at 06:55
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";
}