Java Calendar Month getMonth(Calendar cal)

Here you can find the source of getMonth(Calendar cal)

Description

get Month

License

Open Source License

Declaration

public static String getMonth(Calendar cal) 

Method Source Code

//package com.java2s;

import java.util.Calendar;

public class Main {

    public static String getMonth(Calendar cal) {
        return strLen(String.valueOf(cal.get(Calendar.MONTH) + 1), 2);
    }//from   ww w .  j  av  a2s.  c  om

    public static int getMonth(String s) {
        if (s == null || s.length() < 10) {
            return 1;
        }
        return Integer.parseInt(s.substring(5, 7));
    }

    private static String strLen(String s, int len) {
        if (isNullStr(s)) {
            s = "";
        }
        if (s.length() == 8) {
            return s;
        }
        for (int i = 0; i < len - s.length(); i++) {
            s = "0" + s;
            if (s.length() == 8) {
                break;
            }
        }
        return s;
    }

    private static boolean isNullStr(String s) {
        if (s == null || s.trim().length() <= 0) {
            return true;
        } else {
            return false;
        }
    }
}

Related

  1. getLastDayOfMonth(Calendar c)
  2. getLastDayOfMonth(Calendar c)
  3. getLastDayOfMonth(Calendar calendar)
  4. getLastMonthDays(Calendar c)
  5. getMonth(Calendar c)
  6. getMonth(Calendar calendar)
  7. getMonthBegin(Calendar c)
  8. getMonthBegin(Calendar calendar)
  9. getMonthName(Calendar cal)