Java Day of Month getLastDayOfXMonth(Date date, String xMonth)

Here you can find the source of getLastDayOfXMonth(Date date, String xMonth)

Description

date + x month and get the last day of the month

License

Open Source License

Parameter

Parameter Description
date a parameter
xMonth a parameter

Declaration

public static String getLastDayOfXMonth(Date date, String xMonth) 

Method Source Code

//package com.java2s;
/**// w w w . j  a va  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.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * date + x month and get the last day of the month
     * 
     * @param date
     * @param xMonth
     * @return
     */
    public static String getLastDayOfXMonth(Date date, String xMonth) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DATE, 1);
        cal.add(Calendar.MONTH, Integer.parseInt(xMonth) + 1);
        cal.add(Calendar.DATE, -1);
        return new SimpleDateFormat("yyyyMMdd").format(cal.getTime());
    }

    /**
     * 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();
    }

    /**
     * Get the formatted string of the given date instance based on the
     * date format provided.
     * <p>
     * See {@link java.text.SimpleDateFormat SimpleDateFormat}
     * for examples of the format string.
     * <p>
     * @param  date   The date that needs to be formatted.
     * @param  format The format to be applied to the date.
     * @return The formatted Date String.
     */
    public static String format(Date date, String format) {
        if (date == null || format == null)
            return null;

        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(date);
    }
}

Related

  1. getLastDayOfMonth(java.util.Date date)
  2. getLastDayOfMonth(String year, String month)
  3. getLastdayOfMonth(String year, String month)
  4. getLastDayOfMonth(String year, String month)
  5. getLastDayOfTheMonth(String dateOfString)
  6. getToDate(boolean invoiceSearchFromFirstDayOfMonth)
  7. getWeekOfFirstDayOfMonth(Date date)
  8. getWeekOfLastDayOfMonth(Date date)
  9. isGivenDayOfMonth(java.util.Date date, int dayOfMonth)