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

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

Description

get last day of month

License

Open Source License

Parameter

Parameter Description
year a parameter
month a parameter

Declaration

public static String getLastDayOfMonth(String year, String month) 

Method Source Code

//package com.java2s;
/**/*from   w ww.  j ava 2 s .  c  o  m*/
*  Copyright 2012 NCS Pte. Ltd. All Rights Reserved
*
*  This software is confidential and proprietary to NCS Pte. Ltd. You shall
*  use this software only in accordance with the terms of the license
*  agreement you entered into with NCS.  No aspect or part or all of this
*  software may be reproduced, modified or disclosed without full and
*  direct written authorisation from NCS.
*
*  NCS SUPPLIES THIS SOFTWARE ON AN "AS IS" BASIS. NCS MAKES NO
*  REPRESENTATIONS OR WARRANTIES, EITHER EXPRESSLY OR IMPLIEDLY, ABOUT THE
*  SUITABILITY OR NON-INFRINGEMENT OF THE SOFTWARE. NCS SHALL NOT BE LIABLE
*  FOR ANY LOSSES OR DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
*  MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
*/

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

public class Main {
    /**
     * get last day of month
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, 1);
        cal.add(Calendar.DATE, -1);
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
    }

    /**
     * Date Arithmetic function. Adds the specified (signed) amount of time to
     * the given time field, based on the calendar's rules.
     * <p>
     * For example, to subtract 5 days from a specific date, it can be achieved
     * by calling: <p>
     * DateUtil.add(date, Calendar.DATE, -5).
     * <p>
     * @param date   The date to perform the arithmetic function on
     * @param field  A Calendar constant to retrieve the field value from the Date
     * object. Same as for {@link #get get()}.
     * @param amount the amount of date or time to be added to the field
     * @return The date as a result of the execution of the arithmetic function.
     */
    public static Date add(Date date, int field, int amount) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, amount);

        return cal.getTime();
    }

    /**
     * Retrieves the value of the field in the Date.
     * Some common fields that is likely to be used include :
     * <p>
     * <li>Calendar.YEAR - retrieves the year value
     * <li>Calendar.MONTH - retrieves the month value ( 1 - 12 )
     * <li>Calendar.DAY_OF_MONTH - retrieve the day value ( 1 - 31 )
     * <li>Calendar.HOUR - retrieves the hours value in 12 hour format ( 1 - 12 )
     * <li>Calendar.HOUR_OF_DAY - retrieves the hours value in 24 hour format ( 0 - 23 )
     * <li>Calendar.MINUTE - retrieves the minutes value ( 0 - 59 )
     * <li>Calendar.AM_PM - retrieves the am/pm value ( 0 = am; 1 = pm )
     * <p>
     * @param  date  The Date object to extract value from.
     * @param  field A Calendar constant to retrieve the field value from the Date
     * object.
     * @return The value of the field that is requested.
     * @throws ArrayIndexOutOfBoundsException - if specified field is out of
     * range (<code>field</code> &lt; 0 || <code>field</code> &gt;= <code>Calendar.FIELD_COUNT</code>).
     * @see java.util.Calendar
     */
    public static int get(Date date, int field) {

        Calendar cal = Calendar.getInstance();

        cal.setTime(date);

        int value = cal.get(field);

        // Add 1 if the field is Calendar.MONTH since Calendar returns
        // the month value starting from 0.
        if (Calendar.MONTH == field)
            value += 1;

        // If it is 12 am/pm, the value will be 0. Need to change it to 12 for ease of display.
        if (Calendar.HOUR == field && value == 0)
            value = 12;

        return value;
    }
}

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(java.util.Date date)
  6. getLastdayOfMonth(String year, String month)
  7. getLastDayOfMonth(String year, String month)
  8. getLastDayOfTheMonth(String dateOfString)
  9. getLastDayOfXMonth(Date date, String xMonth)