Returns the number of a month from String - Java java.util

Java examples for java.util:Month

Description

Returns the number of a month from String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String month = "java2s.com";
        System.out.println(getMonthAsInt(month));
    }/* ww w . ja v a2  s.co  m*/

    /**
     * Returns the number of a month.
     * Example: When you put in "January" as a parameter you get 0.
     * @param month Month String.
     * @return number of that month.
     */
    public static int getMonthAsInt(String month) {
        switch (month) {
        case "January":
            return 0;
        case "February":
            return 1;
        case "March":
            return 2;
        case "April":
            return 3;
        case "May":
            return 4;
        case "June":
            return 5;
        case "July":
            return 6;
        case "August":
            return 7;
        case "September":
            return 8;
        case "October":
            return 9;
        case "November":
            return 10;
        case "December":
            return 11;
        default:
            System.out.println("Something wrong in CalendarHelper :"
                    + month);
            return 0;
        }
    }
}

Related Tutorials