Java Month Day isLastDayOfMonth()

Here you can find the source of isLastDayOfMonth()

Description

Checking the date of the end of Month

License

Open Source License

Return

true/false

Declaration

public static boolean isLastDayOfMonth() 

Method Source Code


//package com.java2s;
import java.text.DateFormat;

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

public class Main {
    /**/*from  w  ww.  j ava2s .  c  o  m*/
     * Checking the date of the end of Month
     * @return true/false
     */
    public static boolean isLastDayOfMonth() {
        try {

            Date today = new Date();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(today);
            calendar.add(Calendar.MONTH, 1);
            calendar.set(Calendar.DAY_OF_MONTH, 1);
            calendar.add(Calendar.DATE, -1);

            Date lastDayOfMonth = calendar.getTime();

            DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            //System.out.println("Today            : " + sdf.format(today));
            //System.out.println("Last Day of Month: " + sdf.format(lastDayOfMonth));
            if (sdf.format(today).equals(sdf.format(lastDayOfMonth))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            System.err.println("Checking isLastDayOfMonth error: " + e.getMessage());
            return false;
        }
    }

    public static Date setTime(final Date date, final int hourOfDay, final int minute, final int second) {
        final GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        gc.set(Calendar.HOUR_OF_DAY, hourOfDay);
        gc.set(Calendar.MINUTE, minute);
        gc.set(Calendar.SECOND, second);
        return gc.getTime();
    }
}

Related

  1. getPreviosMonthFirstDay()
  2. getShortFirstDayOfMonth()
  3. getStringOfFirstDayInMonth()
  4. getStringOfFirstDayInMonth()
  5. isFirstDayOfMonth()
  6. isLastDayOfMonth(Calendar cal)
  7. lastDayOfLastMonth()
  8. lastdayofmonth()
  9. lastDayOfMonth(String batdate)