Java Month Name Get month(String monthName)

Here you can find the source of month(String monthName)

Description

Convert a month name (english names) (or a valid abbreviation) to an int in {1,12}.

License

Apache License

Parameter

Parameter Description
monthName The name of the month, in English

Return

the month number, in the range 1-12.

Declaration

public static int month(String monthName) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w  w w. ja va 2 s  .  c o m*/
     * The names of the months
     */
    private static final String[] months = { "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December" };

    /**
     * Convert a month name (english names) (or a valid abbreviation) to an int in {1,12}.
     * Warning: the current implementation uses startsWith(), so month("J") returns 1.
     *
     * @param monthName The name of the month, in English
     * @return the month number, in the range 1-12.
     */
    public static int month(String monthName) {
        for (int i = 0; i < months.length; i++) {
            String m = months[i];
            if (m.startsWith(monthName)) {
                return i + 1;
            }
        }
        return -1;
    }
}

Related

  1. getMonthStr(int month)
  2. getMonthStr(int month)
  3. getMonthStr(String str)
  4. getMonthString(int month)
  5. getPreMonthText(String pattern)
  6. monthCalValue(String monthName)
  7. monthName(int month)
  8. monthName(int n)
  9. monthNameAbbrev(int mon)