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

static Calendar toNearestWholeMinute(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());//from  w  w  w.j av  a 2s . co m

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

    c.set(Calendar.SECOND, 0);

    return c;
}

From source file:Main.java

static Calendar toNearestWholeHour(Calendar d) {
    Calendar c = new GregorianCalendar();
    c.setTime(d.getTime());//w  ww .ja v  a  2  s.  c  o  m

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

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

    return c;
}

From source file:Main.java

/**
 * Adds to a date returning a new object.
 * The original date object is unchanged.
 *
 * @param date  the date, not null/* ww w  .j av a2  s  . c o  m*/
 * @param calendarField  the calendar field to add to
 * @param amount  the amount to add, may be negative
 * @return the new date object with the amount added
 * @throws IllegalArgumentException if the date is null
 */
private static Date add(Date date, int calendarField, int amount) {
    if (date == null) {
        throw new IllegalArgumentException("The date must not be null");
    }
    Calendar c = Calendar.getInstance();
    c.setTime(date);
    c.add(calendarField, amount);
    return c.getTime();
}

From source file:de.otto.mongodb.profiler.web.AbstractController.java

protected static long lowerBoundary(int diffHours) {
    final Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.add(Calendar.HOUR, diffHours);
    return cal.getTimeInMillis();
}

From source file:Main.java

public static Date getNextMonday(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);// w w w. j  ava  2  s  .com
    while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }
    return cal.getTime();
}

From source file:org.zalando.stups.oauth2.spring.client.StupsTokensAccessTokenProviderTest.java

public static Date tomorrow() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, 1);
    return cal.getTime();
}

From source file:org.zalando.stups.oauth2.spring.client.StupsTokensAccessTokenProviderTest.java

public static Date yesterday() {
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_MONTH, -1);
    return cal.getTime();
}

From source file:Main.java

public static Date getNextSaturday(Date date) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*ww  w . j a  v a2s. c  om*/
    while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY) {
        cal.add(Calendar.DAY_OF_WEEK, 1);
    }
    return cal.getTime();
}

From source file:org.pau.assetmanager.viewmodel.chart.ResourceImageGenerator.java

private static void cleanTempFolder(File directory) {
    Calendar cal = Calendar.getInstance();
    int daysBack = 1;
    cal.add(Calendar.DAY_OF_MONTH, daysBack * -1);
    long purgeTime = cal.getTimeInMillis();
    File[] files = directory.listFiles();
    for (File file : files) {
        if (file.lastModified() < purgeTime) {
            file.delete();/*from w  w  w . j a va 2s .  co  m*/
        }
    }
}

From source file:sf.wicketlearningapplication.server.TestUtility.java

public static Bug createNewBugInstance() {
    final int estimatedHours = random.nextInt(8) + 1;
    final Severity severity = Severity.values()[(int) (Math.random() * Severity.values().length)];
    final Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, random.nextInt(30));
    ////  ww  w.j  a  v  a 2s  .c  o  m
    final Bug bug = new Bug();
    bug.setSummary(text());
    bug.setSeverity(severity);
    bug.setEstimatedHours(estimatedHours);
    bug.setDueByDate(calendar.getTime());
    return bug;
}