Java Day End getEndDateOfMonth(Date given)

Here you can find the source of getEndDateOfMonth(Date given)

Description

get End Date Of Month

License

Apache License

Declaration

public static Date getEndDateOfMonth(Date given) 

Method Source Code

    //package com.java2s;
    //License from project: Apache License 

    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Date;

    public class Main {
        public static Date getEndDateOfMonth(Date given) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(given);//  www . j av  a 2s  . com

            int year = calendar.get(Calendar.YEAR);
            int month = calendar.get(Calendar.MONTH);
            int date = calendar.get(Calendar.DATE);

            int eom = 31;

            if (month == 1) {
                if (isLeapYear(year)) {
                    eom = 29;
                } else {
                    eom = 28;
                }
            } else if (isBigMonth(month)) {
                eom = 31;
            } else if (isSmallMonth(month)) {
                eom = 30;
            }

            calendar.set(Calendar.DATE, eom);

            return calendar.getTime();
        }

        private static boolean isLeapYear(int year) {
            return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
        }

        private static boolean isBigMonth(int month) {
    int[] months = {0, 2, 4, 6, 7, 9, 11};
    return Arrays.stream(months).anyMatch(e -> e == month);
}

        private static boolean isSmallMonth(int month) {
    int[] months = {3, 5, 8, 10};
    return Arrays.stream(months).anyMatch(e -> e == month);
}
    }

Related

  1. getEndDate(String date)
  2. getEndDateByDate(Date date)
  3. getEndDateByYears(Date date, int years)
  4. getEndDateForYear()
  5. getEndDateOfCurrentSemester()
  6. getEndMonth(Date sessionEnd, int year, int excessDays)
  7. getEndOfDay(Date d)
  8. getEndOfDay(Date date)
  9. getEndOfDay(Date date)