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

@SuppressWarnings({ "static-access" })
public static String getFutureMonthDay(String iFuntureMonth, String sBirthDay) {
    if (!"".equals(iFuntureMonth) && !"".equals(sBirthDay)) {
        Calendar cal = Calendar.getInstance();
        cal.set(Integer.parseInt(sBirthDay.substring(0, 4)), Integer.parseInt(sBirthDay.substring(5, 7)) - 1,
                Integer.parseInt(sBirthDay.substring(8, 10)));
        cal.add(cal.MONTH, Integer.parseInt(iFuntureMonth));
        return cal.get(cal.YEAR) + "/" + String.format("%02d", (cal.get(cal.MONTH) + 1)) + "/"
                + String.format("%02d", cal.get(cal.DATE));
    } else {/*from  w w w. ja v a 2 s. c o m*/
        return "";
    }
}

From source file:com.github.jgility.core.util.ReleasePlanningUtils.java

private static Calendar addDayOffset(final Calendar date, long offset) {
    Calendar newDate = new GregorianCalendar();
    newDate.setTimeInMillis(date.getTimeInMillis());
    newDate.add(Calendar.DAY_OF_MONTH, safeLongToInt(offset));
    return newDate;
}

From source file:Main.java

public static String convertUtc2LocalPro(String utcTime) {
    if (TextUtils.isEmpty(utcTime)) {
        return "";
    }//from w w  w . j a va 2s .c o  m
    // 2014-10-24T02:58:05.932Z
    SimpleDateFormat utcFormatter, localFormatter;
    utcFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ENGLISH);
    Date date;
    Calendar cal = Calendar.getInstance();
    try {
        date = utcFormatter.parse(utcTime);
        cal.setTime(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    cal.add(Calendar.HOUR_OF_DAY, +8);
    date = cal.getTime();
    localFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    return localFormatter.format(date);
}

From source file:Main.java

public static Date getMidnight(Date date) {
    Calendar day = Calendar.getInstance();
    day.setTime(date);/*from w w w.  j a v a 2 s. c  om*/
    day.set(Calendar.HOUR_OF_DAY, 0);
    day.set(Calendar.MINUTE, 0);
    day.set(Calendar.SECOND, 0);
    day.set(Calendar.MILLISECOND, 0);
    day.add(Calendar.DAY_OF_MONTH, 0);

    return day.getTime();
}

From source file:com.silverpeas.jcrutil.RandomGenerator.java

public static Calendar getOutdatedCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, -(1 + random.nextInt(10)));
    calendar.set(Calendar.HOUR_OF_DAY, getRandomHour());
    calendar.set(Calendar.MINUTE, getRandomMinutes());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.setLenient(false);//from   w  w  w . j  a va2s  . c  o m
    try {
        calendar.getTime();
    } catch (IllegalArgumentException ie) {
        return getOutdatedCalendar();
    }
    return calendar;
}

From source file:com.silverpeas.jcrutil.RandomGenerator.java

public static Calendar getFuturCalendar() {
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DAY_OF_MONTH, 1 + random.nextInt(10));
    calendar.set(Calendar.HOUR_OF_DAY, getRandomHour());
    calendar.set(Calendar.MINUTE, getRandomMinutes());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    calendar.setLenient(false);/*  ww  w.java 2  s  .  com*/
    try {
        calendar.getTime();
    } catch (IllegalArgumentException ie) {
        return getFuturCalendar();
    }
    return calendar;
}

From source file:com.yahoo.dba.perf.myperf.springmvc.MyPerfBaseController.java

/**
 * Date range in yyyyMMddHHmmss format, for metrics and alert retrieval.
 * @param startDate/*  w w w  .  j ava  2 s .c om*/
 * @param endDate
 * @return
 */
public static String[] getDateRange(String startDate, String endDate) {
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMddHHmmss");//metricsDB TS column format
    java.text.SimpleDateFormat sdf2 = new java.text.SimpleDateFormat("yyyy-MM-dd HH");//URL request format
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    sdf2.setTimeZone(TimeZone.getTimeZone("UTC"));
    Date d1 = null;
    if (startDate != null && !startDate.isEmpty()) {
        try {
            if (startDate.indexOf('-') < 0)
                d1 = sdf.parse(startDate);
            else
                d1 = sdf2.parse(startDate);
        } catch (Exception ex) {
        }
    }
    //default to one day earlier
    if (d1 == null) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        d1 = c.getTime();
    }
    String startDateFmt = sdf.format(d1);

    Date d2 = null;
    if (endDate != null && !endDate.isEmpty()) {
        try {
            if (endDate.indexOf('-') < 0)
                d2 = sdf.parse(endDate);
            else
                d2 = sdf2.parse(endDate);
        } catch (Exception ex) {
        }
    }
    //default to now
    if (d2 == null) {
        d2 = new Date();
    }
    String endDateFmt = sdf.format(d2);
    return new String[] { startDateFmt, endDateFmt };
}

From source file:Main.java

public static String getDateByAddHour(String datetime, int minute) {
    String returnTime = null;//from w ww  . j av  a 2 s . co  m
    Calendar cal = new GregorianCalendar();
    SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
        date = ft.parse(datetime);
        cal.setTime(date);
        cal.add(GregorianCalendar.MINUTE, minute);
        returnTime = getFormatDateTime(cal.getTime(), "yyyy-MM-dd HH:mm:ss");
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return returnTime;

}

From source file:de.kurashigegollub.dev.gcatest.Utils.java

public static DateTime getDateTime2Weeks(DateTime dtFrom) {
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(dtFrom.getValue());
    c.add(Calendar.WEEK_OF_YEAR, 2);
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 59);//from   w  ww . ja va2  s.  com
    c.set(Calendar.SECOND, 59);
    c.set(Calendar.MILLISECOND, 999);
    return new DateTime(c.getTime(), TimeZone.getDefault());
}

From source file:com.controlj.experiment.bulktrend.trendclient.TrendClient.java

public static Calendar getYesterday() {
    Calendar today = new GregorianCalendar();
    today.add(Calendar.DATE, -1);
    Calendar result = new GregorianCalendar(today.get(Calendar.YEAR), today.get(Calendar.MONTH),
            today.get(Calendar.DAY_OF_MONTH));
    return result;
}