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:Main.java

public static long getYesterTime() {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    return calendar.getTimeInMillis();
}

From source file:Main.java

public static Date getPreNDay(Date presentDate, int n) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(presentDate);//www . j a  v  a  2 s  . c om
    calendar.add(Calendar.DAY_OF_MONTH, -n);
    presentDate = calendar.getTime();
    return presentDate;
}

From source file:Main.java

public static Date getNextDay(Date presentDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(presentDate);//from  w w w. j a  v  a 2s .  c o m
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    presentDate = calendar.getTime();
    return presentDate;
}

From source file:Main.java

public static Date getPreDay(Date presentDate) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(presentDate);//from w w w  . ja  va2s  .c o  m
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    presentDate = calendar.getTime();
    return presentDate;
}

From source file:Main.java

public static Date addMinutes(Date date, int min) {
    if (date == null) {
        return null;
    }//from   w w  w . j av a2s .  c  om

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);
    calendar.add(Calendar.MINUTE, min);
    return calendar.getTime();
}

From source file:Main.java

/**
 * Get the seven days before date//ww  w .  j a  v  a 2 s  .  co m
 * 
 * @return {@link String}
 */

public static String GetDateSevenDaysBack() {
    DateFormat formatter = new SimpleDateFormat("dd_MMM_yyyy");
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_YEAR, -7);
    return formatter.format(calendar.getTime());

}

From source file:Main.java

public static long getEndOfCurrentWeek() {
    Calendar calendar = getCalendarWithTime(getStartOfCurrentWeek());
    calendar.add(Calendar.DATE, 6);
    return calendar.getTimeInMillis();
}

From source file:Main.java

public static String getDateStr(int index) {
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");

    java.util.Calendar calendar = new java.util.GregorianCalendar();

    calendar.add(Calendar.DATE, index);

    String retDate = "";
    try {/*from   ww  w .  j a  v  a  2  s  .  c om*/
        retDate = df.format(calendar.getTime());
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return retDate;

}

From source file:Main.java

public static String getNextDateByMonth(String s, int i) {

    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
    java.util.Date date = simpledateformat.parse(s, new ParsePosition(0));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/*from  w w w.  j  a v a  2s.  co m*/
    calendar.add(2, i);
    date = calendar.getTime();
    s = simpledateformat.format(date);
    return s;
}

From source file:Main.java

public static String getNextDateByNum(String s, int i) {

    SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyyMMdd");
    java.util.Date date = simpledateformat.parse(s, new ParsePosition(0));
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);//  w  w  w.ja v  a  2  s.c  o  m
    calendar.add(5, i);
    date = calendar.getTime();
    s = simpledateformat.format(date);
    return s;
}