Example usage for org.joda.time DateTime plusMonths

List of usage examples for org.joda.time DateTime plusMonths

Introduction

In this page you can find the example usage for org.joda.time DateTime plusMonths.

Prototype

public DateTime plusMonths(int months) 

Source Link

Document

Returns a copy of this datetime plus the specified number of months.

Usage

From source file:stroom.index.server.IndexShardKeyUtil.java

License:Apache License

private static String getTimeBasedPartitionName(final Index index, final long timeMs) {
    String partition = ALL;/* ww  w . ja va 2 s .c o m*/
    if (index.getPartitionBy() != null && index.getPartitionSize() > 0) {
        DateTime dateTime = new DateTime(timeMs);

        if (PartitionBy.YEAR.equals(index.getPartitionBy())) {
            int year = dateTime.get(DateTimeFieldType.year());
            year = fix(year, index.getPartitionSize());

            dateTime = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
            partition = DateUtil.createFileDateTimeString(dateTime.getMillis());
            partition = partition.substring(0, 4);

        } else if (PartitionBy.MONTH.equals(index.getPartitionBy())) {
            final int year = dateTime.get(DateTimeFieldType.year());
            int month = dateTime.get(DateTimeFieldType.monthOfYear());
            month = fix(month, index.getPartitionSize());
            if (month < 1) {
                month = 1;
            }

            dateTime = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
            dateTime = dateTime.plusMonths(month - 1);
            partition = DateUtil.createFileDateTimeString(dateTime.getMillis());
            partition = partition.substring(0, 7);

        } else if (PartitionBy.WEEK.equals(index.getPartitionBy())) {
            final int year = dateTime.get(DateTimeFieldType.year());
            int week = dateTime.get(DateTimeFieldType.weekOfWeekyear());
            week = fix(week, index.getPartitionSize());
            if (week < 1) {
                week = 1;
            }

            dateTime = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
            dateTime = dateTime.plusWeeks(week - 1);
            partition = DateUtil.createFileDateTimeString(dateTime.getMillis());
            partition = partition.substring(0, 10);

        } else if (PartitionBy.DAY.equals(index.getPartitionBy())) {
            final int year = dateTime.get(DateTimeFieldType.year());
            int day = dateTime.get(DateTimeFieldType.dayOfYear());
            day = fix(day, index.getPartitionSize());
            if (day < 1) {
                day = 1;
            }

            dateTime = new DateTime(year, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC);
            dateTime = dateTime.plusDays(day - 1);
            partition = DateUtil.createFileDateTimeString(dateTime.getMillis());
            partition = partition.substring(0, 10);
        }
    }

    return partition;
}

From source file:test.integ.be.e_contract.sts.ExampleSecurityTokenServiceProvider.java

License:Open Source License

private static X509Certificate getCertificate(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    X500Name subjectName = new X500Name("CN=SAML Signer");
    X500Name issuerName = subjectName; // self-signed
    BigInteger serial = new BigInteger(128, new SecureRandom());
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(issuerName, serial,
            notBefore.toDate(), notAfter.toDate(), subjectName, publicKeyInfo);
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter asymmetricKeyParameter = PrivateKeyFactory.createKey(privateKey.getEncoded());

    ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
            .build(asymmetricKeyParameter);
    X509CertificateHolder x509CertificateHolder = x509v3CertificateBuilder.build(contentSigner);

    byte[] encodedCertificate = x509CertificateHolder.getEncoded();

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(encodedCertificate));
    return certificate;
}

From source file:test.integ.be.e_contract.sts.MyKeyManager.java

License:Open Source License

private static X509Certificate getCertificate(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    X500Name subjectName = new X500Name("CN=localhost");
    X500Name issuerName = subjectName; // self-signed
    BigInteger serial = new BigInteger(128, new SecureRandom());
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(issuerName, serial,
            notBefore.toDate(), notAfter.toDate(), subjectName, publicKeyInfo);
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter asymmetricKeyParameter = PrivateKeyFactory.createKey(privateKey.getEncoded());

    ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
            .build(asymmetricKeyParameter);
    X509CertificateHolder x509CertificateHolder = x509v3CertificateBuilder.build(contentSigner);

    byte[] encodedCertificate = x509CertificateHolder.getEncoded();

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(encodedCertificate));
    return certificate;
}

From source file:test.integ.be.e_contract.sts.saml.SAMLSecurityTokenServiceProvider.java

License:Open Source License

private static X509Certificate getCertificate(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    X500Name subjectName = new X500Name("CN=SAML STS Signer");
    X500Name issuerName = subjectName; // self-signed
    BigInteger serial = new BigInteger(128, new SecureRandom());
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(issuerName, serial,
            notBefore.toDate(), notAfter.toDate(), subjectName, publicKeyInfo);
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter asymmetricKeyParameter = PrivateKeyFactory.createKey(privateKey.getEncoded());

    ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
            .build(asymmetricKeyParameter);
    X509CertificateHolder x509CertificateHolder = x509v3CertificateBuilder.build(contentSigner);

    byte[] encodedCertificate = x509CertificateHolder.getEncoded();

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(encodedCertificate));
    return certificate;
}

From source file:test.integ.be.e_contract.sts.UsernamePasswordSecurityTokenServiceProvider.java

License:Open Source License

private static X509Certificate getCertificate(PrivateKey privateKey, PublicKey publicKey) throws Exception {
    X500Name subjectName = new X500Name("CN=Test");
    X500Name issuerName = subjectName; // self-signed
    BigInteger serial = new BigInteger(128, new SecureRandom());
    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(publicKey.getEncoded());
    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);
    X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(issuerName, serial,
            notBefore.toDate(), notAfter.toDate(), subjectName, publicKeyInfo);
    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter asymmetricKeyParameter = PrivateKeyFactory.createKey(privateKey.getEncoded());

    ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
            .build(asymmetricKeyParameter);
    X509CertificateHolder x509CertificateHolder = x509v3CertificateBuilder.build(contentSigner);

    byte[] encodedCertificate = x509CertificateHolder.getEncoded();

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(encodedCertificate));
    return certificate;
}

From source file:test.integ.be.fedict.hsm.HSMProxyTestCredential.java

License:Open Source License

public static X509Certificate generateSelfSignedCertificate(KeyPair keyPair) throws Exception {
    X500Name issuer = new X500Name("CN=Test");
    X500Name subject = issuer;/*from  w  w  w .j  ava 2  s .com*/

    SecureRandom secureRandom = new SecureRandom();
    byte[] serialValue = new byte[8];
    secureRandom.nextBytes(serialValue);
    BigInteger serial = new BigInteger(serialValue);

    DateTime notBefore = new DateTime();
    DateTime notAfter = notBefore.plusMonths(1);

    SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(keyPair.getPublic().getEncoded());

    X509v3CertificateBuilder x509v3CertificateBuilder = new X509v3CertificateBuilder(issuer, serial,
            notBefore.toDate(), notAfter.toDate(), subject, publicKeyInfo);

    AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA1withRSA");
    AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId);
    AsymmetricKeyParameter asymmetricKeyParameter = PrivateKeyFactory
            .createKey(keyPair.getPrivate().getEncoded());
    ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId)
            .build(asymmetricKeyParameter);
    X509CertificateHolder x509CertificateHolder = x509v3CertificateBuilder.build(contentSigner);

    byte[] encodedCertificate = x509CertificateHolder.getEncoded();

    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate) certificateFactory
            .generateCertificate(new ByteArrayInputStream(encodedCertificate));
    return certificate;
}

From source file:za.co.bayport.rules.PolicyRules.java

/**
 * Checks Acceptable Employment Duration of an Applicant
 * /*ww  w .  j  av  a  2s.  co  m*/
 * @param val
 *            : Date of an Employment @ : Would throw ServiceException containing &lt;errorCode&gt;, &lt;fieldName&gt; and &lt;fieldValue&gt; if: <li>
 *            &lt;val&gt; is empty or null</li> <li>if &lt;val&gt; is less than six months<br/>
 *            <br/>
 *            <b>Suggestion:</b> The applicant must have been employed by his/her current employer for minimum period of (6) months as at the date of
 *            application
 */
public static ServiceException checkEmploymentDuration(Date val) {
    ServiceException ServiceException = checkIsNullOrEmpty("RUL001", "EMPLOYMENT_DURATION", "Is Null Or Empty",
            val);
    DateTime empDate = new DateTime(val);
    if (ServiceException == null) {
        DateTime sixMonthsBeforeDate = new DateTime(new Date());
        sixMonthsBeforeDate = sixMonthsBeforeDate.plusMonths(-6);
        if (empDate.isAfter(sixMonthsBeforeDate))
            ServiceException = new ServiceException("POL005", "EMPLOYMENT_DURATION",
                    "The applicant must have been employed by his/her current employer for minimum period of (6) months as at the date of application");
    }
    return ServiceException;
}