Java Month Get getMonthForString(String monthStr, Locale locale)

Here you can find the source of getMonthForString(String monthStr, Locale locale)

Description

Parses a string (representing a month) and returns it's corresponding value as a Calendar month.

License

Open Source License

Parameter

Parameter Description
monthStr String to parse
locale Locale to use

Return

Month value or -1 if not found

Declaration

private static int getMonthForString(String monthStr, Locale locale) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) Emil Crumhorn - Hexapixel.com - emil.crumhorn@gmail.com
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from w  ww  . j  av  a  2s  .  co  m*/
 *    emil.crumhorn@gmail.com - initial API and implementation
 *******************************************************************************/

import java.text.DateFormatSymbols;

import java.util.Locale;

public class Main {
    /**
     * Parses a string (representing a month) and returns it's corresponding
     * value as a Calendar month. This is used to parse MMM month dates
     * 
     * @param monthStr
     *            String to parse
     * @param locale
     *            Locale to use
     * @return Month value or -1 if not found
     */
    private static int getMonthForString(String monthStr, Locale locale) {
        DateFormatSymbols dfs = new DateFormatSymbols(locale);
        String[] months = dfs.getMonths();
        for (int i = 0; i < months.length; i++) {
            if (months[i].toLowerCase(locale).startsWith(monthStr.toLowerCase(locale))) {
                return i + 1;
            }
        }

        return -1;
    }
}

Related

  1. getMonthEnd(String strdate)
  2. getMonthEndDate(Date date)
  3. getMonthFirstDate(final Date date, final String format)
  4. getMonthForDate(String dateToCheck, String pattern)
  5. getMonthForInt(int num)
  6. getMonthInfo(int monthInfo)
  7. getMonthLength(String countDate)
  8. getMonthNo(Date date)
  9. getMonthNumber()