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 Calendar addDaysIfBefore(Calendar date, Calendar dateBefore, int daysToAdd) {
    while (date.before(dateBefore)) {
        date.add(Calendar.DAY_OF_MONTH, daysToAdd);
    }/*from w  ww .  j a v  a  2s.  c  o  m*/

    return date;
}

From source file:MainClass.java

public static int countDiffDay(Calendar c1, Calendar c2) {
    int returnInt = 0;
    while (!c1.after(c2)) {
        c1.add(Calendar.DAY_OF_MONTH, 1);
        returnInt++;/*from w  w w. j  av a  2s.  co  m*/
    }

    if (returnInt > 0) {
        returnInt = returnInt - 1;
    }

    return (returnInt);
}

From source file:Main.java

public static int getNumberOfDaysInPreviousMonth(Date someDayInCurrentMonth) {
    Calendar lastMonthCal = Calendar.getInstance();
    lastMonthCal.setTime(someDayInCurrentMonth);
    lastMonthCal.add(Calendar.MONTH, -1);
    return lastMonthCal.getActualMaximum(Calendar.DAY_OF_MONTH);
}

From source file:Main.java

public static Date substractMinutes(Date date, int minutes) {
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date);/* w w w .j  a va 2 s  .  com*/
    calendar.add(Calendar.MINUTE, -minutes);
    Date result = calendar.getTime();
    return result;
}

From source file:Main.java

private static void ceil(Calendar cal, int field) {
    cal.add(field, 1);
    cal.add(Calendar.MILLISECOND, -1);
}

From source file:Main.java

/**
 * Get last month first day//from  ww w . j  av a  2s. co  m
 */
public static int getLastMonthFirstDay() {
    Calendar lastDate = Calendar.getInstance();
    lastDate.set(Calendar.DATE, 1);
    lastDate.add(Calendar.MONTH, -1);
    return lastDate.getActualMinimum(Calendar.DAY_OF_MONTH);
}

From source file:com.metamug.mtg.s3.uploader.S3Uploader.java

public static String upload(InputStream inputStream, long fileSize, String URI) {
    String publicURL;//from w w  w. j a  v a2s . com
    //ClientConfiguration max retry
    ObjectMetadata objectMetaData = new ObjectMetadata();
    objectMetaData.setContentLength(fileSize);
    //        objectMetaData.setContentType(IMAGE_CONTENT_TYPE);
    objectMetaData.setCacheControl("public");
    Calendar c = Calendar.getInstance();
    c.setTime(c.getTime());
    c.add(Calendar.MONTH, 6);
    String sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss zzz").format(c.getTime());
    objectMetaData.setHeader("Expires", sdf);//Thu, 21 Mar 2042 08:16:32 GMT

    PutObjectResult por = s3Client
            .putObject(new PutObjectRequest(AWS_S3_BUCKET, URI, inputStream, objectMetaData)
                    .withCannedAcl(CannedAccessControlList.PublicRead));

    publicURL = "http://metamug.net/" + URI;
    return publicURL;
}

From source file:com.streamreduce.util.CAGenerator.java

public static X509Certificate generateCACert(KeyPair keyPair) throws Exception {
    Date startDate = new Date(System.currentTimeMillis()); // time from which certificate is valid
    Calendar expiry = Calendar.getInstance();
    expiry.add(Calendar.DAY_OF_YEAR, 1000 * 365);
    Date expiryDate = expiry.getTime(); // time after which certificate is not valid
    BigInteger serialNumber = new BigInteger(Long.toString(System.currentTimeMillis())); // serial number for certificate

    X509V3CertificateGenerator certGen = new X509V3CertificateGenerator();
    X500Principal dnName = new X500Principal("CN=Nodeable Client");

    certGen.setSerialNumber(serialNumber);
    certGen.setIssuerDN(dnName);/*from ww w.j a  v  a2  s  .  c  o m*/
    certGen.setNotBefore(startDate);
    certGen.setNotAfter(expiryDate);
    certGen.setSubjectDN(dnName);
    certGen.setPublicKey(keyPair.getPublic());
    certGen.setSignatureAlgorithm("MD5withRSA");

    certGen.addExtension(X509Extensions.AuthorityKeyIdentifier, false,
            new AuthorityKeyIdentifierStructure(keyPair.getPublic()));
    certGen.addExtension(X509Extensions.SubjectKeyIdentifier, false,
            new SubjectKeyIdentifierStructure(keyPair.getPublic()));

    return certGen.generate(keyPair.getPrivate()); // note: private key of CA
}

From source file:Main.java

public static PendingIntent scheduleLocalNotification(Context context, int slot, String title, String message,
        Long alertTime, int repeat) {
    Log.i(TAG, "Scheduling local notification");
    Intent alertIntent = new Intent(getNotificationName(slot));

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, slot, alertIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (alarmManager != null) {
        if (repeat == 0) {
            alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, pendingIntent);
        } else {/*from www  .j a  v  a 2  s  .  c o  m*/
            Calendar alarmStartTime = Calendar.getInstance();
            alarmStartTime.add(Calendar.MINUTE, 1);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alertTime, repeat, pendingIntent);
        }
    }
    return pendingIntent;
}

From source file:Main.java

public static Date getDateAdd(Date date, int amount) {
    Calendar cal = new GregorianCalendar();
    cal.setTime(date);/*from  www  . ja va 2 s.c  o  m*/
    cal.add(GregorianCalendar.DATE, amount);
    return cal.getTime();
}