Java Month Convert monthNumber(String s)

Here you can find the source of monthNumber(String s)

Description

returns 1,2,...,12 for the English month name.

License

Open Source License

Parameter

Parameter Description
s the three-letter month name, jan,feb,...,nov,dec

Exception

Parameter Description
ParseException if the name isn't recognized

Return

1,2,...,11,12 for the English month name

Declaration

public static int monthNumber(String s) throws ParseException 

Method Source Code


//package com.java2s;
/*/*from   ww w.java  2s.com*/
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.text.ParseException;

public class Main {
    private final static String[] mons = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct",
            "nov", "dec" };

    /**
     * returns 1,2,...,12 for the English month name.  (Sorry, rest of world...)
     *
     * @param s the three-letter month name, jan,feb,...,nov,dec
     * @return 1,2,...,11,12 for the English month name
     * @throws ParseException if the name isn't recognized
     */
    public static int monthNumber(String s) throws ParseException {
        if (s.length() < 3)
            throw new ParseException("need at least three letters", 0);
        s = s.substring(0, 3);
        for (int i = 0; i < 12; i++) {
            if (s.equalsIgnoreCase(mons[i]))
                return i + 1;
        }
        throw new ParseException("Unable to parse month", 0);
    }
}

Related

  1. month2String(Date date)
  2. monthAgo()
  3. monthConvertToNumber(String str)
  4. monthformMatDate(Date date)
  5. monthIndexAsString(Integer index, Integer offset)
  6. monthOfDate(Date s)
  7. monthsBetween(String from, String to)
  8. monthStringToInt(String month)
  9. nextMonth()