Example usage for org.apache.commons.lang3.time DateUtils addMonths

List of usage examples for org.apache.commons.lang3.time DateUtils addMonths

Introduction

In this page you can find the example usage for org.apache.commons.lang3.time DateUtils addMonths.

Prototype

public static Date addMonths(final Date date, final int amount) 

Source Link

Document

Adds a number of months to a date returning a new object.

Usage

From source file:ubic.basecode.util.DateUtil.java

/**
 * Turn a string like '-7d' into the date equivalent to "seven days ago". Supports 'd' for day, 'h' for hour, 'm'
 * for minutes, "M" for months and "y" for years. Start with a '-' to indicate times in the past ('+' is not
 * necessary for future). Values must be integers.
 * //  w  ww. j a  v a2s  . c o m
 * @param date to be added/subtracted to
 * @param dateString
 * @author Paul Pavlidis
 * @return Date relative to 'now' as modified by the input date string.
 */
public static Date getRelativeDate(Date date, String dateString) {

    if (date == null)
        throw new IllegalArgumentException("Null date");

    Pattern pat = Pattern.compile("([+-]?[0-9]+)([dmhMy])");

    Matcher match = pat.matcher(dateString);
    boolean matches = match.matches();

    if (!matches) {
        throw new IllegalArgumentException(
                "Couldn't make sense of " + dateString + ", please use something like -7d or -8h");
    }

    int amount = Integer.parseInt(match.group(1).replace("+", ""));
    String unit = match.group(2);

    if (unit.equals("h")) {
        return DateUtils.addHours(date, amount);
    } else if (unit.equals("m")) {
        return DateUtils.addMinutes(date, amount);
    } else if (unit.equals("d")) {
        return DateUtils.addDays(date, amount);
    } else if (unit.equals("y")) {
        return DateUtils.addYears(date, amount);
    } else if (unit.equals("M")) {
        return DateUtils.addMonths(date, amount);
    } else {
        throw new IllegalArgumentException(
                "Couldn't make sense of units in " + dateString + ", please use something like -7d or -8h");
    }

}

From source file:userInterface.AuthorityOfficer.ProceedRequestJPanel.java

private void generateJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateJButtonActionPerformed

    //Create License Part
    FireArmLicense license = licenseDirectory
            .searchLicense((LicenseType) licenseTypeJComboBox.getSelectedItem(), holder.getSSN());
    int i = 0;//from www.ja  v a  2  s  .co  m
    if (license == null) {
        license = new FireArmLicense();
        license.setAssignedTo(holder);
        license.setLicensePeriod(Integer.parseInt(licenseJTextField.getText()));
        license.setPrice(Integer.parseInt(priceJTextField.getText()));
        license.setValidTill(DateUtils.addMonths(new Date(), Integer.parseInt(licenseJTextField.getText())));
        license.setType((LicenseType) licenseTypeJComboBox.getSelectedItem());
        licenseDirectory.addLicense(license);
        JOptionPane.showMessageDialog(licenseJTextField, "License Has Been Generated");

    } else {
        //license.setValidFrom(new Date());
        license.setValidTill(DateUtils.addMonths(new Date(), Integer.parseInt(licenseJTextField.getText())));
        license.setUpdatedOn(new Date());
        license.setUpdatedTime(license.getUpdatedTime() + 1);
        JOptionPane.showMessageDialog(licenseJTextField, "License Renewed");
    }

    //JOptionPane.showMessageDialog(licenseJTextField, license.getType());

    //********************************************************************//
    //Edit Work Request part  
    request.setMessage("License Granted");
    request.setStatus("Complete");
    request.setResolveDate(new Date());

    //********************************************************************//
    //Add Transaction
    Transaction transaction = new Transaction();
    transaction.setAmount(Integer.parseInt(priceJTextField.getText()));
    transaction.setType(Transaction.TransactionType.Debit);
    transaction.setStatus("License Transaction - Complete");

    holder.getTransactionHistory().addTransaction(transaction);
    //JOptionPane.showMessageDialog(licenseJTextField, holder.getTransactionHistory().getTransactionList().values());
}

From source file:yoyo.actor.screen.iface.jsf.converter.DateConverter.java

/**
 * Create date.//from ww w .j  a  v  a2s  . c o m
 * @param aDateText date text
 * @return created date
 * @throws ParseException parse exception
 */
private static Date createDate(final String aDateText) throws ParseException {
    final Calendar cal = Calendar.getInstance();
    final Date now = cal.getTime();
    Date date = DateUtils.parseDateStrictly(aDateText,
            new String[] { FORMAL_PATTERN, "yyyyMMdd", "yyMMdd", "MMdd", "dd" });
    if (aDateText.length() == LENGTH_MMDD) {
        date = DateUtils.setYears(date, cal.get(Calendar.YEAR));
        if (date.before(now)) {
            date = DateUtils.addYears(date, 1);
        }
    } else if (aDateText.length() == LENGTH_DD) {
        date = DateUtils.setYears(date, cal.get(Calendar.YEAR));
        date = DateUtils.setMonths(date, cal.get(Calendar.MONTH));
        if (date.before(now)) {
            date = DateUtils.addMonths(date, 1);
        }
    }
    return date;
}

From source file:yoyo.framework.standard.shared.CalendarUtils.java

/**
 * ??//from   w  w  w .j  a  v a  2  s  .  c o  m
 * @param aDate ?
 * @return 
 */
public static Date prevMonth(final Date aDate) {
    return DateUtils.addMonths(aDate, PREV_AMOUNT);
}

From source file:yoyo.framework.standard.shared.CalendarUtils.java

/**
 * ??// www . j a  v  a  2s . c o m
 * @param aDate ?
 * @return 
 */
public static Date nextMonth(final Date aDate) {
    return DateUtils.addMonths(aDate, NEXT_AMOUNT);
}