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 Date toNearestWholeMinute(Date d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d);// w w  w. ja v a 2  s.  c  o  m

    if (c.get(Calendar.SECOND) >= 30)
        c.add(Calendar.MINUTE, 1);

    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    return c.getTime();
}

From source file:Main.java

/**
 * Add date time with almost one month./* ww w . j ava  2  s . com*/
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithAlmostOneMonth(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.DAY_OF_MONTH, isPast ? -30 : 30);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Add date time with ten days.//from   w w w . ja v  a2  s .com
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithTenDays(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.DAY_OF_MONTH, isPast ? -10 : 10);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Add date time with almost one year.//from   w w  w  .ja  v a 2s. c  om
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithAlmostOneYear(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -12 : 12);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Add date time with one day./*from   w ww . j a v  a  2  s .c om*/
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithOneDay(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.DAY_OF_MONTH, isPast ? -1 : 1);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

public static Calendar nextMonday() {
    Calendar date = new GregorianCalendar();
    while (date.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        date.add(Calendar.DATE, 1);
    }/*from   ww  w .  j  av a2s . co  m*/
    date.set(Calendar.HOUR_OF_DAY, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 0);
    date.set(Calendar.MILLISECOND, 0);
    return date;
}

From source file:Main.java

/**
 * Add date time with almost two years./*from   w  w w  . ja  v a  2 s . co m*/
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithAlmostTwoYears(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -10 : 10);
    calendar.add(Calendar.YEAR, isPast ? -1 : 1);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

/**
 * Add date time with five years./* w w  w .  j ava 2 s  .c  o  m*/
 *
 * @param listing     the listing
 * @param currentTime the current time
 * @param isPast      true/false if past time
 */
private static void addDateTimeWithFiveYears(final List<Long> listing, final long currentTime,
        final boolean isPast) {
    final Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(currentTime);
    calendar.add(Calendar.MONTH, isPast ? -10 : 10);
    calendar.add(Calendar.YEAR, isPast ? -5 : 5);
    listing.add(calendar.getTimeInMillis());
}

From source file:Main.java

public static int daysBetween(Calendar startDate, Calendar endDate) {

    Calendar date = (Calendar) startDate.clone();
    long daysBetween = 0;
    while (date.before(endDate)) {
        date.add(Calendar.DAY_OF_MONTH, 1);
        daysBetween++;/*from w  ww . jav  a  2  s.  c o  m*/
    }
    return (int) (daysBetween / 365.25);
}

From source file:Main.java

public static Calendar getFinishDate(String startDate, String duration) {
    try {/*from w ww  .  jav  a2s.  co  m*/
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
        Calendar calendarFinish = Calendar.getInstance();
        calendarFinish.setTime(sdf.parse(startDate));
        calendarFinish.add(Calendar.DATE, Integer.parseInt(duration));
        return calendarFinish;
    } catch (ParseException e) {
        Log.e("PARSE_FAIL", Log.getStackTraceString(e));
    }
    return null;
}