Java Day of Month getLastDayOfMonth(int year, int month)

Here you can find the source of getLastDayOfMonth(int year, int month)

Description

get Last Day Of Month

License

Apache License

Declaration

public static Integer getLastDayOfMonth(int year, int month) 

Method Source Code

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

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

public class Main {

    public static Integer getLastDayOfMonth(int year, int month) {
        return intDateSub(getIntDate(newCalendar(year, month + 1, 1)), 1);
    }/*from   w w  w  . j a  v a 2  s.co m*/

    public static Integer intDateSub(int date, int days) {
        return intDateAdd(date, -days);
    }

    public static Integer getIntDate(int year, int month, int day) {
        return getIntDate(newCalendar(year, month, day));
    }

    public static Integer getIntDate(Calendar c) {
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        return year * 10000 + month * 100 + day;
    }

    public static Integer getIntDate(Date d) {
        if (d == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(d);
        return getIntDate(c);
    }

    public static Calendar newCalendar(int year, int month, int day) {
        Calendar ret = Calendar.getInstance();
        if (year < 100) {
            year = 2000 + year;
        }
        ret.set(year, month - 1, day);
        return ret;
    }

    public static Calendar newCalendar(int date) {
        int year = date / 10000;
        int month = (date % 10000) / 100;
        int day = date % 100;

        Calendar ret = Calendar.getInstance();
        ret.set(year, month - 1, day);
        return ret;
    }

    public static Integer intDateAdd(int date, int days) {
        int year = date / 10000;
        int month = (date % 10000) / 100;
        int day = date % 100;

        day += days;

        return getIntDate(year, month, day);
    }
}

Related

  1. getLastDayOfMonth(int year, int month)
  2. getLastDayOfMonth(int year, int month)
  3. getLastDayOfMonth(int year, int month)
  4. getLastDayOfMonth(int year, int month)
  5. getLastDayOfMonth(int year, int month)
  6. getLastDayOfMonth(int year, int month)
  7. getLastDayOfMonth(java.util.Date date)
  8. getLastDayOfMonth(String year, String month)
  9. getLastDayOfMonth(String year, String month)