简体   繁体   中英

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");
    }
public int getValue(int month) {
    return month % 3 == 0 ? 3 : month % 3;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM