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.bigdata.journal.jini.ha.AbstractHA3BackupTestCase.java

/**
 * We need to set the time at which the {@link DefaultSnapshotPolicy} runs
 * to some point in the future in order to avoid test failures due to
 * violated assumptions when the policy runs up self-triggering (based on
 * the specified run time) during a CI run.
 * <p>//from  w w w.  j  av  a  2  s  .  c  o  m
 * We do this by adding one hour to [now] and then converting it into the
 * 'hhmm' format as an integer.
 * 
 * @return The "never run" time as hhmm.
 */
static protected String getNeverRunSnapshotTime() {

    // Right now.
    final Calendar c = Calendar.getInstance();

    // Plus an hour.
    c.add(Calendar.HOUR_OF_DAY, 1);

    // Get the hour.
    final int hh = c.get(Calendar.HOUR_OF_DAY);

    // And the minutes.
    final int mm = c.get(Calendar.MINUTE);

    // Format as hhmm.
    final String neverRun = "" + hh + (mm < 10 ? "0" : "") + mm;

    return neverRun;

}

From source file:Main.java

public static String futureDate(int add) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    java.util.Date date = null;/*from   ww w  .j ava 2s.  c om*/
    try {
        //date = simpleDateFormat.parse(simpleDateFormat.format(new Date())); 
        java.util.Date currentTime = calendar.getTime();

        calendar.setTime(currentTime);

        // add(Calendar.DAY_OF_MONTH, -5).
        calendar.add(java.util.Calendar.DAY_OF_YEAR, add);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return simpleDateFormat.format(calendar.getTime());

}

From source file:Main.java

public static String previosDate(int sub) {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
    java.util.Calendar calendar = java.util.Calendar.getInstance();
    java.util.Date date = null;/*from   w ww. ja v a  2  s  .  co m*/
    try {
        //date = simpleDateFormat.parse(simpleDateFormat.format(new Date())); 
        java.util.Date currentTime = calendar.getTime();

        calendar.setTime(currentTime);

        // add(Calendar.DAY_OF_MONTH, -5).
        calendar.add(java.util.Calendar.DAY_OF_YEAR, -sub);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return simpleDateFormat.format(calendar.getTime());

}

From source file:net.granoeste.commons.util.DateUtils.java

/**
 * ???//from w  ww  .j a v a  2  s  .c  o  m
 *
 * @param cal
 * @param amount ex) last week : -1. the current week : 0. the next week : 1
 */
public static void shiftDateOnMondayOfTheWeek(final Calendar cal, final int amount) {

    cal.add(Calendar.WEEK_OF_YEAR, amount);
    if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
        cal.add(Calendar.DATE, -6);
    } else {
        cal.add(Calendar.DATE, -cal.get(Calendar.DAY_OF_WEEK) + 2);
    }
}

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

public static String monthFirstDay(int n) {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.MONTH, n);
    return calendar.get(Calendar.YEAR) + "-" + formatNum(calendar.get(Calendar.MONTH) + 1) + "-01";
}

From source file:net.firejack.platform.core.utils.DateUtils.java

/**
 * Truncates the date to week level// w  w w .  ja v  a 2s  .com
 * @param date - date to be truncated
 * @return truncated date
 */
public static Date truncateDateToWeek(Date date) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(DateUtils.truncate(date, Calendar.DAY_OF_MONTH));
    while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
        cal.add(Calendar.DAY_OF_MONTH, -1);
    }
    return cal.getTime();
}

From source file:gobblin.source.extractor.utils.Utils.java

public static Date addDaysToDate(Date datetime, int days) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(datetime);//  www  . ja v  a2 s .c o m
    calendar.add(Calendar.DATE, days);
    return calendar.getTime();
}

From source file:gobblin.source.extractor.utils.Utils.java

public static Date addHoursToDate(Date datetime, int hours) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(datetime);//w w w  .jav a 2  s.  com
    calendar.add(Calendar.HOUR, hours);
    return calendar.getTime();
}

From source file:gobblin.source.extractor.utils.Utils.java

public static Date addSecondsToDate(Date datetime, int seconds) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(datetime);//  w w w .j a v a2s.co  m
    calendar.add(Calendar.SECOND, seconds);
    return calendar.getTime();
}

From source file:com.jabyftw.easiercommands.Util.java

/**
 * Source: Essentials (found through Ban-Management)
 * <b>Letters used:</b> y mo w d h m s
 *
 * @param time string with the time, eg: "3w4h" - three weeks and four hours
 * @return the time in milliseconds/*w  w w . j  av a  2  s. c  o m*/
 * @see <a href=https://github.com/BanManagement/BanManager/blob/master/src/main/java/me/confuser/banmanager/util/DateUtils.java>Credits to Essentials</a>
 */
public static long parseTimeDifference(@NotNull String time) {
    Pattern timePattern = Pattern.compile("(?:([0-9]+)\\s*y[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*mo[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*w[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*d[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*h[a-z]*[,\\s]*)?" + "(?:([0-9]+)\\s*m[a-z]*[,\\s]*)?"
            + "(?:([0-9]+)\\s*(?:s[a-z]*)?)?", Pattern.CASE_INSENSITIVE);
    Matcher matcher = timePattern.matcher(time);

    int years = 0, months = 0, weeks = 0, days = 0, hours = 0, minutes = 0, seconds = 0;
    boolean found = false;

    while (matcher.find()) {
        if (matcher.group() == null || matcher.group().isEmpty()) {
            continue;
        }

        for (int i = 0; i < matcher.groupCount(); i++) {
            if (matcher.group(i) != null && !matcher.group(i).isEmpty()) {
                found = true;
                break;
            }
        }

        if (found) {
            if (matcher.group(1) != null && !matcher.group(1).isEmpty())
                years = Integer.parseInt(matcher.group(1));
            if (matcher.group(2) != null && !matcher.group(2).isEmpty())
                months = Integer.parseInt(matcher.group(2));
            if (matcher.group(3) != null && !matcher.group(3).isEmpty())
                weeks = Integer.parseInt(matcher.group(3));
            if (matcher.group(4) != null && !matcher.group(4).isEmpty())
                days = Integer.parseInt(matcher.group(4));
            if (matcher.group(5) != null && !matcher.group(5).isEmpty())
                hours = Integer.parseInt(matcher.group(5));
            if (matcher.group(6) != null && !matcher.group(6).isEmpty())
                minutes = Integer.parseInt(matcher.group(6));
            if (matcher.group(7) != null && !matcher.group(7).isEmpty())
                seconds = Integer.parseInt(matcher.group(7));
            break;
        }
    }

    if (!found)
        throw new IllegalArgumentException("Date can't be parsed");
    if (years > 20)
        throw new IllegalArgumentException("Date is too big");

    Calendar calendar = new GregorianCalendar();

    if (years > 0)
        calendar.add(Calendar.YEAR, years);
    if (months > 0)
        calendar.add(Calendar.MONTH, months);
    if (weeks > 0)
        calendar.add(Calendar.WEEK_OF_YEAR, weeks);
    if (days > 0)
        calendar.add(Calendar.DAY_OF_MONTH, days);
    if (hours > 0)
        calendar.add(Calendar.HOUR_OF_DAY, hours);
    if (minutes > 0)
        calendar.add(Calendar.MINUTE, minutes);
    if (seconds > 0)
        calendar.add(Calendar.SECOND, seconds);

    return calendar.getTimeInMillis() - System.currentTimeMillis();
}