2

I am getting successfully the months from Calendar like this:

monthMap = calendar.getDisplayNames(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

But the months appear like:

enter image description here

Is there anyway I will get them as January 1st with index 0 and then the rest?

I would sort the list but there is no criteria for the sort.

Any ideas?

matrix
  • 314
  • 4
  • 24

3 Answers3

3

You are getting a map completely unsorted, so you need to implement a sort criteria by the values

 Map<String, Integer> map = Calendar.getInstance().getDisplayNames(Calendar.MONTH, Calendar.LONG,
            Locale.getDefault());
 List<Entry<String, Integer>> months = new ArrayList<>(map.entrySet());
 months.sort((e1, e2) -> Integer.compare(e1.getValue(), e2.getValue()));
 months.stream().forEach(System.out::println);

OR you can consider using the java.time power since java8, because we all should stop learning the old bad/evil/broken java Calendar:

Month month = Month.JUNE;

for (Month m : Month.values()) {
    System.out.println(m);
}

or to keep coherence to java8

Arrays.stream(Month.values()).forEach(System.out::println);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
2

First of all, I'd recommend to stop using Calendar (as already suggested by this answer).

The old classes (Date, Calendar and SimpleDateFormat) have lots of problems and design issues, and they're being replaced by the new APIs.

If you're using Java 8, consider using the new java.time API. It's easier, less bugged and less error-prone than the old APIs.

If you're using Java <= 7, you can use the ThreeTen Backport, a great backport for Java 8's new date/time classes. And for Android, there's the ThreeTenABP (more on how to use it here).

The code below works for both. The only difference is the package names (in Java 8 is java.time and in ThreeTen Backport (or Android's ThreeTenABP) is org.threeten.bp), but the classes and methods names are the same.

For me it's not clear if you want a list with the month names or a map with the months in order (having the month name as key and the number as value). Anyway, here's a code for both (assuming that the default locale is English):

// created the sorted list
List<String> months = new ArrayList<>();
for (Month m : Month.values()) {
    months.add(m.getDisplayName(TextStyle.FULL, Locale.getDefault()));
}
System.out.println("list: " + months);

// create the map (using LinkedHashMap, as it preserves the insertion order)
Map<String, Integer> map = new LinkedHashMap<>();
int i = 0;
for (String m : months) {
    map.put(m, i++);
}
System.out.println("map: " + map);

The output will be:

list: [January, February, March, April, May, June, July, August, September, October, November, December]

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

Community
  • 1
  • 1
  • 2
    Good Answer. I would emphasize more the direct use of the `Month` enum objects directly. I suspect the original poster is using Strings to track the months as entities for more work rather than just the name of the month for presentation. With a proper understanding of enum objects, the OP can learn to pass around `Month` objects as arguments rather than strings, and can learn to use the enum objects as keys in maps or items in lists, and as values in member variables on classes. In other words, use `Month` everywhere with the only month-related string being generated for presentation to user. – Basil Bourque Jun 20 '17 at 19:17
1

You just need to find a way to sort it. Chances are you're getting back a HashMap, which doesn't promise any order.

Map<String, Integer> map = Calendar.getInstance()
    .getDisplayNames(Calendar.MONTH, Calendar.LONG, Locale.getDefault());

List<Entry<String, Integer>> entries = 
    new ArrayList<Entry<String, Integer>>(map.entrySet()); 

//This line sorts entries by key.
Collections.sort(entries, 
    (entry1, entry2) -> entry1.getKey().compareTo(entry2.getKey()));

//Now the list will be sorted:
for(Entry<String, Integer> entry: entries) {
    System.out.println(entry.getKey() + " = " + entry.getValue());
}

For numeric sort, you can replace the call to sort with

Collections.sort(entries, 
    (entry1, entry2) -> Integer.valueOf(entry1.getKey()).compareTo(Integer.valueOf(entry2.getKey())));
ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • With this they'll be sorted, but alphabetically. – Andy Turner Jun 20 '17 at 15:59
  • thanks for the try. I created my own hashMap with getting items from DateFormatSymbols which returns the items with the method: [getMonths()](https://developer.android.com/reference/java/text/DateFormatSymbols.html#getMonths()) – matrix Jun 20 '17 at 16:10