6

I'm trying to get all 12 months into a String list in java. How can I do this using the java.util calender. I've used the following code to build a year list! now I want is to build a month list for the 12 months.

protected String getYears(){
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 3);

        int futureYear = cal.get(Calendar.YEAR);

        List<String> years = new ArrayList<String>();

        for(int i = -2 ; i < 1 ; i++){
            if(i != 0){
                cal = Calendar.getInstance();
                cal.add(Calendar.YEAR,i);
                years.add(Integer.toString(cal.get(Calendar.YEAR)));
            }else if(currentYear != futureYear){
                years.add(Integer.toString(currentYear));
                years.add(Integer.toString(futureYear));
            }else if(currentYear == futureYear){
                years.add(Integer.toString(currentYear));
            }
        }


        HTMLOptionBuilder ob = new HTMLOptionBuilder(false);
        for(int i = 0; i < years.size(); i++){
            if(years.get(i).equals(currentYear)){
                ob.addOption(years.get(i), years.get(i), true, true);
            }else{
                ob.addOption(years.get(i), years.get(i));
            }
        }
        return ob.getHTML();
    }

please help me! thank you!

Imesh Chandrasiri
  • 5,558
  • 15
  • 60
  • 103
  • I cannot use another plugin! I'm asking for java util! – Imesh Chandrasiri May 30 '13 at 04:57
  • Your description and your code do not seem to match. What is all this future year stuff? [do you want this](http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html#getDisplayName%28int,%20int,%20java.util.Locale%29) – BevynQ May 30 '13 at 05:00

4 Answers4

18

try this

import java.text.DateFormatSymbols;

public class Main {
  public static void main(String[] args) {
   List<String> monthsList = new ArrayList<String>();
    String[] months = new DateFormatSymbols().getMonths();
    for (int i = 0; i < months.length; i++) {
      String month = months[i];
      System.out.println("month = " + month);
      monthsList .add(months[i]);
    }

DateFormatSymbols

PSR
  • 39,804
  • 41
  • 111
  • 151
7

This uses 100 less characters than PSR's solution. :)

List<String> months = Arrays.asList("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

Also I think this is more immediately obvious, but doesn't work if you want locales or other weird things.

Zong
  • 6,160
  • 5
  • 32
  • 46
4

Calendar.getDisplayNames(int field, int style, Locale locale) gives you a Map of month names and their corresponding integer values.

e.g the following -

Calendar cal = Calendar.getInstance();
Map<String, Integer> map = cal.getDisplayNames(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH);
System.out.println(map);

should display something as follows -

{October=9, January=0, April=3, February=1, August=7, June=5, November=10, July=6, May=4, December=11, March=2, September=8}

You can use the map to form the list as you like.

Note: This is just in case you should use something from java.util, although I like the one presented by @PSR better, which seems to be the most straight forward (and also supports locale).

Community
  • 1
  • 1
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1
     import java.text.DateFormatSymbols;
     import java.util.Locale;

public class MonthNames {
  public static void main(String[] args) {
    String[] months = new DateFormatSymbols().getMonths();
    for (int i = 0; i < months.length; i++) {
        String month = months[i];
        System.out.println("month = " + month);
    }

    String[] shortMonths = new DateFormatSymbols().getShortMonths();
    for (int i = 0; i < shortMonths.length; i++) {
        String shortMonth = shortMonths[i];
        System.out.println("shortMonth = " + shortMonth);
    }

    String[] germanyMonths = new DateFormatSymbols(Locale.GERMANY).getMonths();
    for (int i = 0; i < germanyMonths.length; i++) {
        String germanyMonth = germanyMonths[i];
        System.out.println("germanyMonth = " + germanyMonth);
    }

    String[] germanyShortMonths = new DateFormatSymbols(Locale.GERMANY).getShortMonths();
    for (int i = 0; i < germanyShortMonths.length; i++) {
        String germanyShortMonth = germanyShortMonths[i];
        System.out.println("germanyShortMonth = " + germanyShortMonth);
    }
}
}

Reference Get Months Example

  • 1
    What's odd about the getMonths() method is that it returns an array of length 13, with the last element being empty. Is this a bug? – Aquarelle Sep 11 '14 at 20:23