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
- month2String(Date date)
- monthAgo()
- monthConvertToNumber(String str)
- monthformMatDate(Date date)
- monthIndexAsString(Integer index, Integer offset)
- monthOfDate(Date s)
- monthsBetween(String from, String to)
- monthStringToInt(String month)
- nextMonth()