Example usage for java.util GregorianCalendar setTime

List of usage examples for java.util GregorianCalendar setTime

Introduction

In this page you can find the example usage for java.util GregorianCalendar setTime.

Prototype

public final void setTime(Date date) 

Source Link

Document

Sets this Calendar's time with the given Date.

Usage

From source file:uk.nhs.cfh.dsp.srth.demographics.person.utils.PatientUtilsServiceImpl.java

public Patient createRandomPatient(int minAge) {

    GregorianCalendar gc = new GregorianCalendar(1900, 0, 1);
    long centuryStart = gc.getTimeInMillis();
    // rewind time to now - 1
    GregorianCalendar gc2 = new GregorianCalendar();
    gc2.setTime(Calendar.getInstance().getTime());
    gc2.add(Calendar.YEAR, -minAge);
    long todayMinusMinYears = gc2.getTimeInMillis();

    // use a random value to generate values between century start and today
    long randomTime = (long) (Math.random() * (todayMinusMinYears - centuryStart)) + centuryStart;
    // use random time to set calendar
    gc.setTimeInMillis(randomTime);//from  w  ww.  java  2s  .  co  m

    // create and return patient with dob as random time
    return createRandomPatient(gc);
}

From source file:uk.nhs.cfh.dsp.srth.demographics.person.utils.PatientUtilsServiceImpl.java

public Calendar getRandomDateInTimeRangeInFuture(Calendar anchor, int yearUpperBound, int yearLowerBound,
        int monthUpperBound, int monthLowerBound, int dayUpperBound, int dayLowerBound) {

    int randomYear = (int) (Math.random() * (yearUpperBound - yearLowerBound)) + yearLowerBound;
    int randomMonth = (int) (Math.random() * (monthUpperBound - monthLowerBound)) + monthLowerBound;
    int randomDay = (int) (Math.random() * (dayUpperBound - dayLowerBound)) + dayLowerBound;

    // forward calendar by year and month
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(anchor.getTime());
    calendar.add(Calendar.YEAR, randomYear);
    calendar.add(Calendar.MONTH, randomMonth);
    calendar.add(Calendar.DATE, randomDay);
    logger.debug("Value of calendar.get(year) : " + calendar.get(Calendar.YEAR));

    return calendar;
}

From source file:org.tolven.gen.model.GenMedical.java

/**
* Return a time relative to now//from   w ww .j av a  2s  . c o  m
*/
public Date monthsAgo(int months) {
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(getNow());
    cal.add(GregorianCalendar.MONTH, -months);
    return cal.getTime();
}

From source file:test.integ.be.e_contract.mycarenet.tarification.TarificationClientTest.java

@Test
public void testTarificationConsultation() throws Exception {
    // STS/*from w  w  w .  j a v a2 s  .  c  o m*/
    EHealthSTSClient client = new EHealthSTSClient(
            "https://services-acpt.ehealth.fgov.be/IAM/Saml11TokenService/Legacy/v1");

    Security.addProvider(new BeIDProvider());
    KeyStore keyStore = KeyStore.getInstance("BeID");
    BeIDKeyStoreParameter beIDKeyStoreParameter = new BeIDKeyStoreParameter();
    beIDKeyStoreParameter.addPPDUName("digipass 870");
    beIDKeyStoreParameter.addPPDUName("digipass 875");
    beIDKeyStoreParameter.addPPDUName("digipass 920");
    keyStore.load(beIDKeyStoreParameter);
    PrivateKey authnPrivateKey = (PrivateKey) keyStore.getKey("Authentication", null);
    X509Certificate authnCertificate = (X509Certificate) keyStore.getCertificate("Authentication");

    KeyStore eHealthKeyStore = KeyStore.getInstance("PKCS12");
    FileInputStream fileInputStream = new FileInputStream(this.config.getEHealthPKCS12Path());
    eHealthKeyStore.load(fileInputStream, this.config.getEHealthPKCS12Password().toCharArray());
    Enumeration<String> aliasesEnum = eHealthKeyStore.aliases();
    String alias = aliasesEnum.nextElement();
    X509Certificate eHealthCertificate = (X509Certificate) eHealthKeyStore.getCertificate(alias);
    PrivateKey eHealthPrivateKey = (PrivateKey) eHealthKeyStore.getKey(alias,
            this.config.getEHealthPKCS12Password().toCharArray());

    List<Attribute> attributes = new LinkedList<>();
    attributes.add(new Attribute("urn:be:fgov:identification-namespace",
            "urn:be:fgov:ehealth:1.0:certificateholder:person:ssin"));
    attributes.add(new Attribute("urn:be:fgov:identification-namespace", "urn:be:fgov:person:ssin"));

    List<AttributeDesignator> attributeDesignators = new LinkedList<>();
    attributeDesignators.add(new AttributeDesignator("urn:be:fgov:identification-namespace",
            "urn:be:fgov:ehealth:1.0:certificateholder:person:ssin"));
    attributeDesignators
            .add(new AttributeDesignator("urn:be:fgov:identification-namespace", "urn:be:fgov:person:ssin"));
    attributeDesignators.add(new AttributeDesignator("urn:be:fgov:certified-namespace:ehealth",
            "urn:be:fgov:person:ssin:nurse:boolean"));

    Element assertion = client.requestAssertion(authnCertificate, authnPrivateKey, eHealthCertificate,
            eHealthPrivateKey, attributes, attributeDesignators);

    assertNotNull(assertion);

    String assertionString = client.toString(assertion);

    // Tarification
    TarificationClient tarificationClient = new TarificationClient(
            "https://services-acpt.ehealth.fgov.be/MyCareNet/Tarification/v1");
    tarificationClient.setCredentials(eHealthPrivateKey, assertionString);

    ObjectFactory objectFactory = new ObjectFactory();
    SendRequestType sendRequest = objectFactory.createSendRequestType();

    DatatypeFactory datatypeFactory = DatatypeFactory.newInstance();
    GregorianCalendar issueInstantCal = new GregorianCalendar();
    DateTime issueInstantDateTime = new DateTime();
    issueInstantCal.setTime(issueInstantDateTime.toDate());
    XMLGregorianCalendar issueInstant = datatypeFactory.newXMLGregorianCalendar(issueInstantCal);
    sendRequest.setIssueInstant(issueInstant);

    // TODO...

    tarificationClient.tarificationConsultation(sendRequest);

}

From source file:uk.nhs.cfh.dsp.srth.demographics.person.utils.PatientUtilsServiceImpl.java

public Calendar getSensibleRandomDateInTimeRangeRelativeToPatient(Patient patient, int yearUpperBound,
        int yearLowerBound, int monthUpperBound, int monthLowerBound, int dayUpperBound, int dayLowerBound) {

    Calendar anchor = Calendar.getInstance();
    /*//w  w  w. j  a v  a  2s .co m
       * use time now as anchor point and get a random sensible date
       */
    Calendar randomCal = getSensibleRandomDateInTimeRange(anchor, yearUpperBound, yearLowerBound,
            monthUpperBound, monthLowerBound, dayUpperBound, dayLowerBound);
    Date randomDate = randomCal.getTime();
    // we set time now to NOW - 1 day
    Date dob = patient.getDob().getTime();
    GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(dob);
    // reset time by 1 day
    gc.add(Calendar.DATE, 1);
    dob = gc.getTime();
    while (randomDate.before(dob)) {
        randomCal = getSensibleRandomDateInTimeRange(anchor, yearUpperBound, yearLowerBound, monthUpperBound,
                monthLowerBound, dayUpperBound, dayLowerBound);
        // reset random date
        randomDate = randomCal.getTime();
    }

    return randomCal;
}

From source file:org.getobjects.appserver.publisher.GoResource.java

@Override
public void appendToResponse(final WOResponse _r, final WOContext _ctx) {
    URLConnection con = null;// w w  w . j  a  va  2 s . c  om
    try {
        con = this.url.openConnection();
    } catch (IOException coe) {
        log.warn("could not open connection to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* open stream */

    InputStream is = null;
    try {
        is = con.getInputStream();
    } catch (IOException ioe) {
        log.warn("could not open stream to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* transfer */

    try {
        String mimeType = con.getContentType();
        if (mimeType == null || "content/unknown".equals(mimeType))
            mimeType = mimeTypeForPath(this.url.getPath());

        if (mimeType == null)
            mimeType = "application/octet-stream";

        _r.setHeaderForKey(mimeType, "content-type");
        _r.setHeaderForKey("" + con.getContentLength(), "content-length");

        /* setup caching headers */

        Date now = new Date();
        GregorianCalendar cal = new GregorianCalendar();

        cal.setTime(new Date(con.getLastModified()));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "last-modified");

        cal.setTime(now);
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "date");

        cal.add(Calendar.SECOND, this.expirationIntervalForMimeType(mimeType));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "expires");

        /* start streaming */

        _r.enableStreaming();

        byte[] buffer = new byte[0xFFFF];
        for (int len; (len = is.read(buffer)) != -1;)
            _r.appendContentData(buffer, len);
    } catch (IOException e) {
        log.error("IO error trying to deliver resource: " + this.url, e);
        _r.setStatus(WOMessage.HTTP_STATUS_INTERNAL_ERROR);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            log.warn("could not close URL input stream: " + this.url, e);
        }
    }
}

From source file:org.getobjects.appserver.publisher.JoResource.java

@Override
public void appendToResponse(WOResponse _r, WOContext _ctx) {
    URLConnection con = null;//  www .j a v a 2  s .  c  o  m
    try {
        con = this.url.openConnection();
    } catch (IOException coe) {
        log.warn("could not open connection to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* open stream */

    InputStream is = null;
    try {
        is = con.getInputStream();
    } catch (IOException ioe) {
        log.warn("could not open stream to url: " + this.url);
        _r.setStatus(WOMessage.HTTP_STATUS_NOT_FOUND);
        return;
    }

    /* transfer */

    try {
        String mimeType = con.getContentType();
        if (mimeType == null || "content/unknown".equals(mimeType))
            mimeType = mimeTypeForPath(this.url.getPath());

        if (mimeType == null)
            mimeType = "application/octet-stream";

        _r.setHeaderForKey(mimeType, "content-type");
        _r.setHeaderForKey("" + con.getContentLength(), "content-length");

        /* setup caching headers */

        Date now = new Date();
        GregorianCalendar cal = new GregorianCalendar();

        cal.setTime(new Date(con.getLastModified()));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "last-modified");

        cal.setTime(now);
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "date");

        cal.add(Calendar.SECOND, this.expirationIntervalForMimeType(mimeType));
        _r.setHeaderForKey(WOMessage.httpFormatDate(cal), "expires");

        /* start streaming */

        _r.enableStreaming();

        byte[] buffer = new byte[0xFFFF];
        for (int len; (len = is.read(buffer)) != -1;)
            _r.appendContentData(buffer, len);
    } catch (IOException e) {
        log.error("IO error trying to deliver resource: " + this.url, e);
        _r.setStatus(WOMessage.HTTP_STATUS_INTERNAL_ERROR);
    } finally {
        try {
            if (is != null)
                is.close();
        } catch (IOException e) {
            log.warn("could not close URL input stream: " + this.url, e);
        }
    }
}

From source file:uk.nhs.cfh.dsp.srth.demographics.person.utils.PatientUtilsServiceImpl.java

public Calendar getRandomDateInTimeRange(Calendar anchor, int yearUpperBound, int yearLowerBound,
        int monthUpperBound, int monthLowerBound, int dayUpperBound, int dayLowerBound) {

    int randomYear = (int) (Math.random() * (yearUpperBound - yearLowerBound)) + yearLowerBound;
    int randomMonth = (int) (Math.random() * (monthUpperBound - monthLowerBound)) + monthLowerBound;
    int randomDay = (int) (Math.random() * (dayUpperBound - dayLowerBound)) + dayLowerBound;

    // debugging output
    if (logger.isDebugEnabled()) {
        logger.debug("Value of anchor year: " + anchor.get(Calendar.YEAR));
        logger.debug("Value of anchor month: " + anchor.get(Calendar.MONTH));
        logger.debug("Value of anchor date: " + anchor.get(Calendar.DATE));
        logger.debug("Value of randomYear : " + randomYear);
        logger.debug("Value of randomMonth : " + randomMonth);
        logger.debug("Value of randomDay : " + randomDay);
    }/*  w w  w .  jav a 2s .c  o m*/

    // forward calendar by year and month
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(anchor.getTime());
    calendar.add(Calendar.YEAR, -randomYear);
    calendar.add(Calendar.MONTH, -randomMonth);
    calendar.add(Calendar.DATE, -randomDay);
    if (logger.isDebugEnabled()) {
        logger.debug("Value of calendar.get(year) : " + calendar.get(Calendar.YEAR));
    }
    return calendar;
}

From source file:de.unikassel.puma.openaccess.sword.PumaData.java

/**
 * @param phdoralexam the phdoralexam to set
 *//*from  w w w .j av  a2  s.  com*/
public void setPhdoralexam(String phdoralexamString) {
    // convert string to date

    SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
    Date phdoralexamDate = null;
    XMLGregorianCalendar phdoralexamXMLDate = null;

    try {
        phdoralexamDate = sdf.parse(phdoralexamString);
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(phdoralexamDate);
        try {
            phdoralexamXMLDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(c);
        } catch (DatatypeConfigurationException e) {
            log.warn("DatatypeConfigurationException");
        }
    } catch (ParseException e) {
        // Quellzeit hat ein falsches Format
    }

    this.phdoralexam = phdoralexamXMLDate;
}

From source file:org.wso2.appserver.integration.tests.multitenancy.MultiTenancyTestCase.java

@Test(groups = "wso2.as", description = "Test adding duplicate tenant", dependsOnMethods = "testTenantLogin", expectedExceptions = {
        TenantMgtAdminServiceExceptionException.class }, expectedExceptionsMessageRegExp = "TenantMgtAdminServiceExceptionException")
public void testAddingDuplicateTenant() throws Exception {
    Date date = new Date();
    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);
    TenantInfoBean newTenantInfoBean = new TenantInfoBean();
    newTenantInfoBean.setActive(true);//from  w w w.  j  a  v  a2  s.c om
    newTenantInfoBean.setEmail(FIRST_TENANT_USER + "@" + FIRST_TENANT_DOMAIN);
    newTenantInfoBean.setAdmin(FIRST_TENANT_USER);
    newTenantInfoBean.setAdminPassword("admin123");
    newTenantInfoBean.setUsagePlan("Demo");
    newTenantInfoBean.setLastname(FIRST_TENANT_USER + "wso2automation");
    newTenantInfoBean.setSuccessKey("true");
    newTenantInfoBean.setCreatedDate(calendar);
    newTenantInfoBean.setTenantDomain(FIRST_TENANT_DOMAIN);
    newTenantInfoBean.setFirstname(FIRST_TENANT_USER);

    tenantManagementServiceClient.addTenant(newTenantInfoBean);
}