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:com.googlecode.commons.swing.util.DateUtils2.java

public static Date getEndOfMonth(Date month) {
    if (month == null) {
        return null;
    }//from w w  w.j  av  a2s. c  o  m
    Date end = (Date) month.clone();
    end = DateUtils.truncate(end, Calendar.MONTH);
    end = DateUtils.addMonths(end, 1);
    end = DateUtils.addMilliseconds(end, -1);
    return end;
}

From source file:MFPIM.NotificationFrame.java

private void initEmpNotifTable() {
    try {//from w  ww .  j  a  va 2 s.  com

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = DateUtils.addMonths(new Date(), -5);
        String newDate = dateFormat.format(date);

        String sql = "select empNo,fName,lName from Employee where dateHired <= '" + newDate
                + "' and empStatus='Probationary'";
        conn = DBConnect.connect();
        pst = conn.prepareStatement(sql);
        rs = pst.executeQuery();
        probEmpNotifTable.setModel(DbUtils.resultSetToTableModel(rs));
        ChangeName(probEmpNotifTable, 0, "Employee ID No.");
        ChangeName(probEmpNotifTable, 1, "First Name");
        ChangeName(probEmpNotifTable, 2, "Surname");
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "initEmpNotifTable:" + e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, ex);
            }
        }
    }
}

From source file:com.stratelia.webactiv.util.DateUtilTest.java

@Test
public void testGetOutputDateAndHour() {
    Date date = DateUtil.resetHour(java.sql.Date.valueOf("2013-05-21"));
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/21"));

    Date year = DateUtils.addYears(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(year, LANGUAGE), is("2014/05/21"));

    Date month = DateUtils.addMonths(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(month, LANGUAGE), is("2013/06/21"));

    Date day = DateUtils.addDays(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(day, LANGUAGE), is("2013/05/22"));

    Date hour = DateUtils.addHours(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("2013/05/21 01:00"));
    hour = DateUtils.addHours(date, 12);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("2013/05/21 12:00"));
    hour = DateUtils.addHours(date, 22);
    assertThat(DateUtil.getOutputDateAndHour(hour, LANGUAGE), is("2013/05/21 22:00"));

    Date minute = DateUtils.addMinutes(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 00:01"));
    minute = DateUtils.addMinutes(date, 59);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 00:59"));
    minute = DateUtils.addMinutes(date, 60);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 01:00"));
    minute = DateUtils.addMinutes(date, 61);
    assertThat(DateUtil.getOutputDateAndHour(minute, LANGUAGE), is("2013/05/21 01:01"));

    Date second = DateUtils.addSeconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:00"));
    second = DateUtils.addSeconds(date, 59);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:00"));
    second = DateUtils.addSeconds(date, 60);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:01"));
    second = DateUtils.addSeconds(date, 61);
    assertThat(DateUtil.getOutputDateAndHour(second, LANGUAGE), is("2013/05/21 00:01"));

    Date millisecond = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 999);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 1000);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));
    millisecond = DateUtils.addMilliseconds(date, 1001);
    assertThat(DateUtil.getOutputDateAndHour(millisecond, LANGUAGE), is("2013/05/21 00:00"));

    // 2013-05-21 23:59:59.999
    date = DateUtils.addHours(//from  w  ww  . ja v a  2s  .c  o  m
            DateUtils.addMinutes(DateUtils.addSeconds(DateUtils.addMilliseconds(date, 999), 59), 59), 23);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/21 23:59"));

    // 2013-05-22 00:00:00.000
    date = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/22"));

    // 2013-05-22 00:00:00.001
    date = DateUtils.addMilliseconds(date, 1);
    assertThat(DateUtil.getOutputDateAndHour(date, LANGUAGE), is("2013/05/22 00:00"));
}

From source file:com.inkubator.common.util.DateTimeUtil.java

/**
 *
 * Get or return Past or Future date based on parameter</p>
 * <p>/*  w w  w  .j a va 2s . c  o  m*/
 * <b>Parameter amount :</b> use negative symbol to get past date, use
 * positive symbol to get future date. Parameter amount combination with
 * parameter constantParameter. </p>
 * <p>
 * <b>Parameter constantParameter :</b> the type of times that will be
 * added. Example : CommonUtilConstant.DATE_FORMAT_MONTH</p>
 * <p>
 * <b>ConstantParameter type :</b></p>
 * <ul>
 * <li>CommonUtilConstant.DATE_FORMAT_MILLISECOND = millisecond</li>
 * <li>CommonUtilConstant.DATE_FORMAT_SECOND = second</li>
 * <li>CommonUtilConstant.DATE_FORMAT_MINUTES = minutes</li>
 * <li>CommonUtilConstant.DATE_FORMAT_HOURS = hours</li>
 * <li>CommonUtilConstant.DATE_FORMAT_DAY = day</li>
 * <li>CommonUtilConstant.DATE_FORMAT_WEEK = week</li>
 * <li>CommonUtilConstant.DATE_FORMAT_MONTH = month</li>
 * <li>CommonUtilConstant.DATE_FORMAT_YEAR = year</li>
 * </ul>
 *
 * @return Date type of past or future date
 * @param inputParam Date reference to calculate
 * @param timeDifference Integer reference, can be negative value like -7 or
 * positive value like 7
 * @param constantParameter String reference,see the CommonUtilConstant
 */
public static Date getDateFrom(Date inputParam, int timeDifference, String constantParameter) {
    Date returnDate = null;
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_DAY)) {
        returnDate = DateUtils.addDays(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_HOURS)) {
        returnDate = DateUtils.addHours(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_MILLISECOND)) {
        returnDate = DateUtils.addMilliseconds(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_MINUTES)) {
        returnDate = DateUtils.addMinutes(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_MONTH)) {
        returnDate = DateUtils.addMonths(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_SECOND)) {
        returnDate = DateUtils.addSeconds(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_YEAR)) {
        returnDate = DateUtils.addYears(inputParam, timeDifference);
    }
    if (constantParameter.equalsIgnoreCase(CommonUtilConstant.DATE_FORMAT_WEEK)) {
        returnDate = DateUtils.addWeeks(inputParam, timeDifference);
    }
    return returnDate;
}

From source file:entity.Customer.java

public void setFinishDate(Date finishDate) {
    if (finishDate == null) {

        this.finishDate = DateUtils.addMonths(new Date(), 2);

        //            System.out.println(finishDate);
    } else {// ww w  .ja v  a  2 s  .c om
        this.finishDate = finishDate;
    }
}

From source file:com.webbfontaine.valuewebb.report.PDInformationForAnalysisReport.java

@Override
public boolean prepareParameters() {

    if (bean.getFromYear().compareTo(bean.getToYear()) > 0
            || bean.getFromYear().compareTo(bean.getToYear()) == 0
                    && bean.getFromMonth().compareTo(bean.getToMonth()) > 0) {
        infoHandling.setInfoList(Utils.translate("Incorrect interval! Please correct and try again"));
        return false;
    }//from w  ww  .ja  v  a  2  s  .  com

    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
    try {
        fromYearAndMonth.setValue(sdf.parse(bean.getFromYear() + bean.getFromMonth()));

        Date toDate = sdf.parse(bean.getToYear() + bean.getToMonth());

        toDate = DateUtils.addMonths(toDate, 1);
        toDate = DateUtils.addDays(toDate, -1);

        toYearAndMonth.setValue(toDate);
    } catch (ParseException e) {
        LOGGER.warn("", e);
        return false;
    }
    appCountry.setValue(ApplicationProperties.getAppCountry());
    reportLocale.setValue(FR.equals(getLanguage()) ? Locale.FRENCH : Locale.ENGLISH);
    return true;
}

From source file:com.progym.custom.fragments.FoodProgressMonthlyLineFragment.java

@Click
void ivPrevYear() {
    ivPrevYear.startAnimation(fadeIn);//from   w  ww . ja  v a 2 s . c  o  m

    rightOut.setDuration(1000);
    rightOut.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            DATE = DateUtils.addMonths(DATE, -1);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setLineData3(DATE, true);
        }
    });
    graphView.startAnimation(rightOut);
}

From source file:com.progym.custom.fragments.CalloriesProgressMonthlyLineFragment.java

@Click
void ivPrevYear() {
    ivPrevYear.startAnimation(fadeIn);//from   www . jav a  2 s .c om

    rightOut.setDuration(1000);
    rightOut.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            DATE = DateUtils.addMonths(DATE, -1);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setLineData3(DATE, true);

        }
    });
    mChartView.startAnimation(rightOut);

}

From source file:com.progym.custom.fragments.FoodProgressMonthlyLineFragment.java

@Click
void ivNextYear() {
    ivNextYear.startAnimation(fadeIn);//from   w ww. j a  v a2s  .  c o  m
    leftOut.setDuration(1000);
    leftOut.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            DATE = DateUtils.addMonths(DATE, 1);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setLineData3(DATE, false);
        }
    });
    graphView.startAnimation(leftOut);
}

From source file:com.progym.custom.fragments.CalloriesProgressMonthlyLineFragment.java

@Click
void ivNextYear() {
    ivNextYear.startAnimation(fadeIn);/*from   w w w  . j a  v a  2s .  c  om*/

    leftOut.setDuration(1000);
    leftOut.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            DATE = DateUtils.addMonths(DATE, 1);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            setLineData3(DATE, false);
        }
    });
    mChartView.startAnimation(leftOut);
}