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 int getMonthDayNumber(int offset) {
    Calendar calendar = Calendar.getInstance();
    if (offset != 0)
        calendar.add(Calendar.MONTH, offset);
    return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

private static String formatPeriodDate(int daysPeriod) {
    Calendar now = Calendar.getInstance();
    Calendar period = Calendar.getInstance();
    period.add(Calendar.DAY_OF_MONTH, daysPeriod);
    SimpleDateFormat sdf = new SimpleDateFormat("MMMM", Locale.US);

    if (now.get(Calendar.YEAR) == period.get(Calendar.YEAR)) {
        if (now.get(Calendar.MONTH) == period.get(Calendar.MONTH)) {
            return period.get(Calendar.DAY_OF_MONTH) + "-" + now.get(Calendar.DAY_OF_MONTH) + " "
                    + sdf.format(now.getTime()) + ", " + now.get(Calendar.YEAR);
        }//from   ww w .  j  a v a2s . c  o  m
        return period.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(period.getTime()) + " - "
                + now.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(now.getTime()) + ", "
                + now.get(Calendar.YEAR);
    }
    return period.get(Calendar.DAY_OF_MONTH) + " " + sdf.format(period.getTime()) + " "
            + period.get(Calendar.YEAR) + " - " + now.get(Calendar.DAY_OF_MONTH) + " "
            + sdf.format(now.getTime()) + " " + now.get(Calendar.YEAR);
}

From source file:Main.java

@SuppressLint("NewApi")
public static KeyPair generateRsaPairWithGenerator(Context ctx, String alais)
        throws InvalidAlgorithmParameterException, NoSuchProviderException, NoSuchAlgorithmException {
    Calendar notBefore = Calendar.getInstance();
    Calendar notAfter = Calendar.getInstance();
    notAfter.add(1, Calendar.YEAR);
    KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(ctx).setAlias(alais)
            .setSubject(new X500Principal(String.format("CN=%s, OU=%s", alais, ctx.getPackageName())))
            .setSerialNumber(BigInteger.ONE).setStartDate(notBefore.getTime()).setEndDate(notAfter.getTime())
            .build();/* w w w  .  j ava2  s .c o  m*/

    KeyPairGenerator kpGenerator = KeyPairGenerator.getInstance("RSA", "AndroidKeyStore");
    kpGenerator.initialize(spec);
    KeyPair kp = kpGenerator.generateKeyPair();

    return kp;
}

From source file:Main.java

public static String getYesterdayFormattedTime() {

    Calendar calendar = getCurrentCalendar();
    calendar.add(Calendar.HOUR_OF_DAY, -9);
    calendar.add(Calendar.DAY_OF_MONTH, -1);
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz");
    String result = format.format(calendar.getTime());
    String standardName = "";
    if (result.contains("JST")) {
        standardName = "JST";
        result = result.replaceAll(standardName, "+09:00");
    } else if (result.contains("GMT")) {
        standardName = "GMT";
        result = result.replaceAll(standardName, "");
    }// ww w. j  a v  a  2s  .c om
    return result;
}

From source file:Main.java

private static Date getDistanceDate(Date date, int day) {
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);/*from  ww w.  j a  v a 2  s. c  o m*/
    cal.add(Calendar.DATE, day);
    return cal.getTime();
}

From source file:Main.java

public static String getCurrentDateByOffset(String format, int calendarField, int offset) {
    String mDateTime = null;/*w ww.j  av a2  s.  c o  m*/
    try {
        SimpleDateFormat mSimpleDateFormat = new SimpleDateFormat(format);
        Calendar c = new GregorianCalendar();
        c.add(calendarField, offset);
        mDateTime = mSimpleDateFormat.format(c.getTime());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mDateTime;

}

From source file:Main.java

public static int getUpMonthDays(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/*from www .ja  v  a2s .c  o m*/
    c.add(Calendar.MONTH, -1);
    return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static Date add(Date date, int field, int amount) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);/*from w  w  w .  j  av a  2s. co m*/
    c.add(field, amount);
    return c.getTime();
}

From source file:Main.java

public static int getNextMonthDays(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);//from   ww w .j a va  2s .c o m
    c.add(Calendar.MONTH, 1);
    return c.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static int getUpMonth(Date date) {
    Calendar c = Calendar.getInstance();
    c.setTime(date);//from   w  w w . ja  va2s  .co m
    c.add(Calendar.MONTH, -1);
    return c.get(Calendar.MONTH);
}