get Day Index - Java java.util

Java examples for java.util:Day

Description

get Day Index

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int day = 2;
        int month = 2;
        int year = 2;
        System.out.println(getDayIndex(day, month, year));
    }/* w w w .  ja  va2 s  . c o  m*/

    public static int getDayIndex(int day, int month, int year) {
        return day + getDaysInMonthAggregate(month, year);
    }

    private static int getDaysInMonthAggregate(int month, int year) {
        if (month < 1 || month > 12)
            return -1;
        int ans = 0;
        switch (month) {
        case 12:
            ans += getDaysInMonth(11, year);
        case 11:
            ans += getDaysInMonth(10, year);
        case 10:
            ans += getDaysInMonth(9, year);
        case 9:
            ans += getDaysInMonth(8, year);
        case 8:
            ans += getDaysInMonth(7, year);
        case 7:
            ans += getDaysInMonth(6, year);
        case 6:
            ans += getDaysInMonth(5, year);
        case 5:
            ans += getDaysInMonth(4, year);
        case 4:
            ans += getDaysInMonth(3, year);
        case 3:
            ans += getDaysInMonth(2, year);
        case 2:
            ans += getDaysInMonth(1, year);
        default:
            ans += 0;
        }
        return ans;
    }

    private static int getDaysInMonth(int month, int year) {
        if (month < 1 || month > 12)
            return -1;
        switch (month) {
        case 1:
            return 31;
        case 2:
            if (year % 4 == 0)
                return 29;
            else
                return 28;
        case 3:
            return 31;
        case 4:
            return 30;
        case 5:
            return 31;
        case 6:
            return 30;
        case 7:
            return 31;
        case 8:
            return 31;
        case 9:
            return 30;
        case 10:
            return 31;
        case 11:
            return 30;
        case 12:
            return 31;
        default:
            return -1;
        }
    }
}

Related Tutorials