Get the Java Gregorian Calendar value based on the matching month string. - Android java.util

Android examples for java.util:Month

Description

Get the Java Gregorian Calendar value based on the matching month string.

Demo Code


//package com.java2s;

import java.util.Calendar;

public class Main {
    public static void main(String[] argv) {
        String month = "java2s.com";
        System.out.println(convertToMonthValue(month));
    }/*from   ww w . j a va  2 s.  com*/

    /**
     * Get the Java Gregorian Calendar value based on the matching month string. 
     * @param month The month of the calendar. Only the first three letters are compared.
     * @return The Java value of the Gregorian Calendar.
     */
    public static int convertToMonthValue(String month) {
        // Get the first three letters of the month.
        String shortMonth;
        shortMonth = month.toLowerCase().substring(0, 3);

        // Could use a switch case statement here, but the String version isn't compatible with Android...
        if (shortMonth.equals("jan"))
            return Calendar.JANUARY;
        else if (shortMonth.equals("feb"))
            return Calendar.FEBRUARY;
        else if (shortMonth.equals("mar"))
            return Calendar.MARCH;
        else if (shortMonth.equals("apr"))
            return Calendar.APRIL;
        else if (shortMonth.equals("may"))
            return Calendar.MAY;
        else if (shortMonth.equals("jun"))
            return Calendar.JUNE;
        else if (shortMonth.equals("jul"))
            return Calendar.JULY;
        else if (shortMonth.equals("aug"))
            return Calendar.AUGUST;
        else if (shortMonth.equals("sep"))
            return Calendar.SEPTEMBER;
        else if (shortMonth.equals("oct"))
            return Calendar.OCTOBER;
        else if (shortMonth.equals("nov"))
            return Calendar.NOVEMBER;
        else if (shortMonth.equals("dec"))
            return Calendar.DECEMBER;
        else
            return Calendar.UNDECIMBER;
    }
}

Related Tutorials