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.taobao.tddl.sample.util.DateUtil.java

/**
 * //*from  w  w  w.  java2s. co  m*/
 *
 * @param intervals
 * @param format
 * @return
 */
public static String getDiffDate(int intervals, String format) {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DATE, intervals);
    Date intervalDay = cal.getTime();
    return formatDate(intervalDay, format);
}

From source file:Main.java

public static Date getNextdayDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*from  w  w  w  .  jav  a 2  s  .  c o  m*/
    calendar.add(Calendar.DATE, 1);
    return calendar.getTime();
}

From source file:Main.java

public static Date getLastdayDate(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*from  w  w w . j a v  a2s . c o m*/
    calendar.add(Calendar.DATE, -1);
    return calendar.getTime();
}

From source file:Main.java

public static String getLastDayOfThisMonth() {
    String str = "";
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(Calendar.DATE, 1);//
    lastDate.add(Calendar.MONTH, 1);//
    lastDate.add(Calendar.DATE, -1);//

    str = sdf.format(lastDate.getTime());
    return str;/*  ww  w. j  a  v  a 2  s .  co m*/
}

From source file:Main.java

private static int elapsed(Calendar before, Calendar after, int field) {
    Calendar clone = (Calendar) before.clone();
    int elapsed = -1;
    while (!clone.after(after)) {
        clone.add(field, 1);
        elapsed++;/*from  w  ww. j  av  a  2  s.  co m*/
    }
    return elapsed;
}

From source file:Main.java

public static Date nextMonth(Date date) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(getFirstDate(date));
    calendar.add(Calendar.MONTH, 1);
    return calendar.getTime();
}

From source file:Main.java

public static Date startOfNextDay(Date origDate) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(origDate);/*from   www  .  j  ava2 s .  c  o  m*/
    cal.add(Calendar.DATE, 1);

    return startOfDay(cal.getTime());
}

From source file:Main.java

public static Calendar getMonday(Calendar calendar) {
    while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        calendar.add(Calendar.DATE, -1);
    }//  ww w.  ja v a  2 s .  c o m

    return calendar;
}

From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.util.DateUtils.java

public static Date getDateAgo(Unit unit, int amount) {
    Validate.inclusiveBetween(0, Integer.MAX_VALUE, amount);

    Calendar calendar = new GregorianCalendar();
    calendar.add(unit.getCalendarField(), -amount);
    return calendar.getTime();
}

From source file:Main.java

public static String dateformat(String format, Date date, int afterDay) {
    Calendar ca = Calendar.getInstance();
    ca.setTime(date);/*from ww w  .j av a  2  s. c om*/
    if (afterDay != 0)
        ca.add(Calendar.DATE, afterDay);
    SimpleDateFormat df = new SimpleDateFormat(format);
    return df.format(ca.getTime());
}