Java Day of Month getLastDayOfMonth()

Here you can find the source of getLastDayOfMonth()

Description

get Last Day Of Month

License

Apache License

Declaration

public static String getLastDayOfMonth() 

Method Source Code

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

import java.text.SimpleDateFormat;
import java.util.Calendar;

public class Main {

    public static String getLastDayOfMonth() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        String last = sdf.format(ca.getTime());
        return last;
    }/*w w  w .j a  v  a 2  s.  com*/

    private static int getLastDayOfMonth(int year, int month) {
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return 31;
        }
        if (month == 4 || month == 6 || month == 9 || month == 11) {
            return 30;
        }
        if (month == 2) {
            if (isLeapYear(year)) {
                return 29;
            } else {
                return 28;
            }
        }
        return 0;
    }

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

Related

  1. getLastDayOfCurMonth()
  2. getLastDayOfLastMonth()
  3. getlastDayOfMonth()
  4. getLastdayofMonth()
  5. getLastDayOfMonth()
  6. getLastDayOfMonth(Calendar c)
  7. getLastDayOfMonth(Date currDate)
  8. getLastDayOfMonth(Date date)
  9. getLastDayOfMonth(Date date)