Sara Eva
Sara Eva

Reputation: 13

How to get today's date as quarterly divided

Today's date will be given and

January, April, July, October = 1

February, May, August, November = 2

March, June, September, December = 3

should be printed. Example: december is 3rd month of quarter so answer will be 3. Is there any better solution for this?

public int getValue() {
    switch (LocalDate.now().getMonthValue()) {
        case 1:
        case 4:
        case 7:
        case 10:
            return 1;
        case 2:
        case 5:
        case 8:
        case 11:
            return 2;
        case 3:
        case 6:
        case 9:
        case 12:
            return 3;
        default:
            throw new IllegalStateException("Unexpected value");
    }

Upvotes: 0

Views: 85

Answers (1)

hua
hua

Reputation: 169

public int getValue(int month) {
    return month % 3 == 0 ? 3 : month % 3;
}

Upvotes: 2

Related Questions