Example usage for java.util Calendar add

List of usage examples for java.util Calendar add

Introduction

In this page you can find the example usage for java.util Calendar add.

Prototype

public abstract void add(int field, int amount);

Source Link

Document

Adds or subtracts the specified amount of time to the given calendar field, based on the calendar's rules.

Usage

From source file:com.tohours.imo.util.TohoursUtils.java

/**
 * ??n/*from   www  .  j  a v a  2s  .c  om*/
 * @param n
 * @return
 */
public static String getYearMonth(int n) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, n);
    String yearMonth = calendar.get(Calendar.YEAR) + "" + formatNum(calendar.get(Calendar.MONTH) + 1)
            + "";
    return yearMonth;
}

From source file:it.eng.spagobi.engines.chart.bo.charttypes.blockcharts.TimeBlockChart.java

public static Date addDay(Date date) {
    //TODO you may want to check for a null date and handle it.
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from  ww w .  j  a va2s.  c om
    cal.add(Calendar.DATE, 1);
    return cal.getTime();
}

From source file:de.mpg.escidoc.services.reporting.ReportFHI.java

public static String[] getStartEndDateOfQuery() {

    SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-");
    int months;//from  w ww  .ja  v  a  2s  .  c o  m
    try {
        months = new Integer(rprops.getProperty("FHI.report.months.range")).intValue();
    } catch (Exception e) {
        throw new RuntimeException("Cannot read/convert FHI.report.months.range:", e);
    }
    ;

    //from
    Calendar fromMonth = GregorianCalendar.getInstance();
    fromMonth.add(Calendar.MONTH, -months);
    String fromYearMonth = dateformatter.format(fromMonth.getTime());

    //to
    Calendar toMonth = GregorianCalendar.getInstance();
    toMonth.add(Calendar.MONTH, -1);
    String toYearMonth = dateformatter.format(toMonth.getTime());

    return new String[] {
            fromYearMonth + String.format("%02d", fromMonth.getActualMinimum(Calendar.DAY_OF_MONTH)),
            toYearMonth + toMonth.getActualMaximum(Calendar.DAY_OF_MONTH) };
}

From source file:de.mpg.mpdl.inge.reporting.ReportFHI.java

public static String[] getStartEndDateOfQuery() {

    SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy-MM-");
    int months;/*from  w  w w  . ja va  2s  .c  o m*/
    try {
        months = new Integer(rprops.getProperty("FHI.report.months.range")).intValue();
    } catch (Exception e) {
        throw new RuntimeException("Cannot read/convert FHI.report.months.range:", e);
    }
    ;

    // from
    Calendar fromMonth = GregorianCalendar.getInstance();
    fromMonth.add(Calendar.MONTH, -months);
    String fromYearMonth = dateformatter.format(fromMonth.getTime());

    // to
    Calendar toMonth = GregorianCalendar.getInstance();
    toMonth.add(Calendar.MONTH, -1);
    String toYearMonth = dateformatter.format(toMonth.getTime());

    return new String[] {
            fromYearMonth + String.format("%02d", fromMonth.getActualMinimum(Calendar.DAY_OF_MONTH)),
            toYearMonth + toMonth.getActualMaximum(Calendar.DAY_OF_MONTH) };
}

From source file:com.nabla.dc.server.handler.fixed_asset.Asset.java

static public void dispose(final Connection conn, final Integer assetId, final IDisposal disposal)
        throws SQLException, DispatchException {
    final PreparedStatement redo = conn
            .prepareStatement("INSERT INTO fa_transaction_redo (fa_asset_id, command) VALUES(?,?);");
    try {//  w  w w .j av  a  2  s.  co  m
        redo.setInt(1, assetId);
        // backup transaction after disposal if any
        if (log.isDebugEnabled())
            log.debug("backing up transactions after disposal date");
        // charge monthly depreciation in disposal month if disposal is after 15
        final Calendar dt = Util.dateToCalendar(disposal.getDate());
        if (dt.get(GregorianCalendar.DAY_OF_MONTH) >= dt.getActualMaximum(GregorianCalendar.DAY_OF_MONTH) / 2)
            dt.add(GregorianCalendar.MONTH, 1);
        dt.set(GregorianCalendar.DAY_OF_MONTH, 1);
        final Date from = Util.calendarToSqlDate(dt);
        // get list of transactions to backup before we delete them
        final IntegerSet transIds = new IntegerSet();
        final PreparedStatement stmt = StatementFormat.prepare(conn,
                "SELECT t.*" + " FROM fa_transaction AS t INNER JOIN period_end AS p ON t.period_end_id=p.id"
                        + " WHERE t.fa_asset_id=? AND p.end_date>?;",
                assetId, from);
        try {
            final ResultSet rs = stmt.executeQuery();
            try {
                while (rs.next()) {
                    transIds.add(rs.getInt("id"));
                    final String command = MessageFormat.format("INSERT INTO fa_transaction"
                            + " (id,fa_asset_id,period_end_id,amount,class,type,depreciation_period)"
                            + " VALUES({0,number,0},{1,number,0},{2,number,0},{3,number,0},''{4}'',''{5}'',{6,number,0});",
                            rs.getInt("id"), rs.getInt("fa_asset_id"), rs.getInt("period_end_id"),
                            rs.getInt("amount"), rs.getString("class"), rs.getString("type"),
                            Database.getInteger(rs, "depreciation_period"));
                    if (log.isTraceEnabled())
                        log.trace("redo = " + command);
                    redo.setString(2, command);
                    redo.addBatch();
                }
            } finally {
                rs.close();
            }
        } finally {
            stmt.close();
        }
        // remove any transaction after disposal date
        if (log.isDebugEnabled())
            log.debug("removing transactions after disposal date");
        Database.executeUpdate(conn, "DELETE FROM fa_transaction WHERE id IN (?);", transIds);
        // add disposal transactions
        if (log.isDebugEnabled())
            log.debug("adding transactions for disposal");
        final TransactionList transactions = new TransactionList(assetId);
        // closing cost
        transactions.add(new Transaction(TransactionClasses.COST, TransactionTypes.CLOSING, disposal.getDate(),
                -1 * getAssetCostBeforeDisposal(conn, assetId)));
        // closing accumulated depreciation
        transactions.add(new Transaction(TransactionClasses.DEP, TransactionTypes.CLOSING, disposal.getDate(),
                -1 * getAssetDepreciationBeforeDisposal(conn, assetId)));
        for (Integer newTransId : transactions.save(conn, true)) {
            redo.setString(2,
                    MessageFormat.format("DELETE FROM fa_transaction WHERE id={0,number,0};", newTransId));
            redo.addBatch();
        }
        if (!Database.isBatchCompleted(redo.executeBatch()))
            throw new InternalErrorException("failed to save disposal transactions");
    } finally {
        redo.close();
    }
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ????X/*w w w .j  a v  a2 s.  com*/
 *
 * @param offset
 * @return
 */
public static String getOffsetDate(int offset) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, offset);
    String currentDate = DateFormatUtils.format(cal, DATE_FORMAT);
    return currentDate;
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ???X/*from   w  ww.j a  v a 2 s .  co m*/
 *
 * @param specifiedDate ,?YYYY-MM-DD
 * @param offset        ??
 * @return
 * @throws ParseException
 */
public static String getSpecifiedOffsetDate(String specifiedDate, int offset) throws ParseException {
    Date date = DateUtils.parseDate(specifiedDate, DATE_FORMAT);
    Calendar cal = DateUtils.toCalendar(date);
    cal.add(Calendar.DAY_OF_MONTH, offset);
    String returnDate = DateFormatUtils.format(cal, DATE_FORMAT);
    return returnDate;
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ???X/*ww w  .  j av a  2 s  . c  om*/
 *
 * @param specifiedTime ,?yyyy-MM-dd HH:mm:ss
 * @param offset        ??
 * @return
 * @throws ParseException
 */
public static String getSpecifiedOffsetTime(String specifiedTime, int offset) throws ParseException {
    Date date = DateUtils.parseDate(specifiedTime, TIME_FORMAT);
    Calendar cal = DateUtils.toCalendar(date);
    cal.add(Calendar.DAY_OF_MONTH, offset);
    String returnDate = DateFormatUtils.format(cal, TIME_FORMAT);
    return returnDate;
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ??/*from www  .  j a  v  a2s .c  o  m*/
 *
 * @return
 */
public static String getPreviousMonthDate() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -1); // ?
    calendar.add(Calendar.MONTH, -1); // ?

    String currentDate = DateFormatUtils.format(calendar, DATE_FORMAT);
    return currentDate;
}

From source file:com.kongwu.insweb.utils.DateUtils.java

public static Date addDay(Date dtime, int days) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(dtime);//from www.  j  a  va2  s .c om
    calendar.add(Calendar.DAY_OF_MONTH, days);
    return calendar.getTime();
}