get Days Of Month - Java java.util

Java examples for java.util:Month

Description

get Days Of Month

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int year = 2;
        int month = 2;
        System.out.println(getDaysOfMonth(year, month));
    }// www  . j av  a  2s  . co  m

    public static int getDaysOfMonth(int year, int month) {
        int daysOfMonth = -1;

        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysOfMonth = 31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysOfMonth = 30;
            break;
        case 2:
            if (isLeapYear(year)) {
                daysOfMonth = 29;
            } else {
                daysOfMonth = 28;
            }

        }
        return daysOfMonth;
    }

    public static boolean isLeapYear(int year) {
        if (year % 100 == 0 && year % 400 == 0) {
            return true;
        } else if (year % 100 != 0 && year % 4 == 0) {
            return true;
        }
        return false;
    }
}

Related Tutorials