Java Month Number Get getMonthIndex(String str)

Here you can find the source of getMonthIndex(String str)

Description

simple little function to get the index of the month.

License

Open Source License

Parameter

Parameter Description
str a string which (should be) a month

Return

one based index

Declaration

private static int getMonthIndex(String str) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    private static final String strMonths[] = { "January", "February",
            "March", "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };

    /**//  w w w  .j a v a 2 s  . c o  m
     * simple little function to get the index of the month.
     * compare just the first three letters to avoid confusion with
     * misspellings and whether or not abreviations have a period.
     * also allow for 1 based indexs
     * @param str a string which (should be) a month
     * @return one based index
     */
    private static int getMonthIndex(String str) {
        int ix;
        int iValue;

        if (str == null || str.length() == 0)
            return (-1);
        str = str.trim();
        if (str.length() > 3)
            str = str.substring(0, 3);
        for (ix = 0; ix < strMonths.length; ++ix) {
            if (str.equalsIgnoreCase(strMonths[ix].substring(0, 3)))
                return (ix);
        }

        // if the month value does not match one of the three letter
        // abreviations it may be the 1 based index of the month

        try {
            iValue = Integer.parseInt(str);
        } catch (NumberFormatException nfe) {
            return (-1);
        }

        while (iValue < 1)
            iValue += 12;
        while (iValue > 12)
            iValue -= 12;
        return (iValue - 1);
    }
}

Related

  1. eemonth(double serial_number)
  2. getInitialsByMonthNumber(String monthNumber)
  3. getMonthDigit(String month)
  4. getMonthIds()
  5. getMonthIndexAsString(String type)
  6. getMonthNumberByMonthInitials(String monthInitials)
  7. getMonthNumberString(int month)
  8. getMonthNumberString(String sMonth)