Java Month Name Get getMonth(boolean name)

Here you can find the source of getMonth(boolean name)

Description

Returns either the name or the month number depending on the name parameter specified.

License

Open Source License

Parameter

Parameter Description
name if true returns the digit, otherwise returns the name of the month.

Return

either the name or the month number depending on the parameter specified.

Declaration

public static String getMonth(boolean name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 Synopsys, Incorporated
 * 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   ww  w .  ja  v a2  s .c  om
 * Synopsys, Inc - Initial implementation 
 *******************************************************************************/

import java.util.Calendar;

public class Main {
    private static Calendar mCalendar = null;
    private static String[] months = new String[] { "January", "February", "March", "April", "May", "June", "July",
            "August", "September", "October", "November", "December" };

    /**
     * Returns either the name or the month number depending on the name parameter specified. N.B. If the month number
     * is returned, it will be a digit from 1-12.
     * @param name if true returns the digit, otherwise returns the name of the month.
     * @return either the name or the month number depending on the parameter specified.
     */
    public static String getMonth(boolean name) {
        if (mCalendar == null)
            Init();
        if (name)
            return months[mCalendar.get(Calendar.MONTH)];
        else {
            String retVal = null;
            int val = mCalendar.get(Calendar.MONTH) + 1;
            if (val < 10)
                retVal = "0" + val;
            else
                retVal = val + "";
            return retVal;
        }
    }

    /**
     * This method creates a new Calendar object
     */
    public static void Init() {
        mCalendar = Calendar.getInstance();
    }
}

Related

  1. createDaemonThread(Runnable runnable, String threadName)
  2. getDutchMonthName(int monthNumber)
  3. getMonth(String monthName)
  4. getMonthByName(String monthName)
  5. getMonthFromMonthName(String name)
  6. getMonthName()