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:org.odk.collect.android.http.HttpClientConnection.java

private static void setOpenRosaHeaders(HttpRequest req) {
    req.setHeader(OPEN_ROSA_VERSION_HEADER, OPEN_ROSA_VERSION);
    GregorianCalendar gregorianCalendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
    gregorianCalendar.setTime(new Date());
    req.setHeader(DATE_HEADER, DateFormat.format("E, dd MMM yyyy hh:mm:ss zz", gregorianCalendar).toString());
}

From source file:eu.europa.ec.markt.dss.DSSUtils.java

/**
 * Converts a given <code>Date</code> to a new <code>XMLGregorianCalendar</code>.
 *
 * @param date the date to be converted/*from  w  w w .ja va 2 s .  c  om*/
 * @return the new <code>XMLGregorianCalendar</code> or null
 */
public static XMLGregorianCalendar createXMGregorianCalendar(Date date) {

    if (date == null) {
        return null;
    }

    final GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(date);

    try {
        XMLGregorianCalendar gc = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
        gc.setFractionalSecond(null);
        gc = gc.normalize(); // to UTC = Zulu
        return gc;
    } catch (DatatypeConfigurationException e) {

        // LOG.log(Level.WARNING, "Unable to properly convert a Date to an XMLGregorianCalendar",e);
    }

    return null;
}

From source file:org.nuclos.common2.SeriesUtils.java

/**
 * /*from  ww w . j a  v  a2  s  . c  om*/
 * @param dateFrom
 * @param dateUntil
 * @return
 */
public static List<DateTime> getPossibleDates(String series, DateTime dateFrom, DateTime dateUntil) {
    List<DateTime> result = new ArrayList<DateTime>();

    if (dateUntil.before(dateFrom)) {
        return result;
    }

    final GregorianCalendar calendar = new GregorianCalendar();

    DateTime dateCalculated = getSeriesNext(series, dateFrom);
    while (dateCalculated.before(dateUntil)) {
        result.add(dateCalculated);

        calendar.setTime(dateCalculated.getDate());
        calendar.add(GregorianCalendar.MINUTE, 1);
        dateCalculated = getSeriesNext(series, new DateTime(calendar.getTime()));
    }

    return result;
}

From source file:com.emcopentechnologies.viprcloudstorage.WAStorageClient.java

/**
 * Generates SAS URL for blob in Cloud storage account
 * @param storageAccountName//from  w ww .  java  2s .  c om
 * @param storageAccountKey
 * @param containerName
 * @param strBlobURL
 * @return SAS URL
 * @throws Exception
 */
public static String generateSASURL(String storageAccountName, String storageAccountKey, String containerName,
        String saBlobEndPoint) throws Exception {
    StorageCredentialsAccountAndKey credentials = new StorageCredentialsAccountAndKey(storageAccountName,
            storageAccountKey);
    URL blobURL = new URL(saBlobEndPoint);
    String saBlobURI = new StringBuilder().append(blobURL.getProtocol()).append("://")
            .append(storageAccountName).append(".").append(blobURL.getHost()).append("/").toString();
    CloudStorageAccount cloudStorageAccount = new CloudStorageAccount(credentials, new URI(saBlobURI),
            new URI(getCustomURI(storageAccountName, QUEUE, saBlobURI)),
            new URI(getCustomURI(storageAccountName, TABLE, saBlobURI)));
    // Create the blob client.
    CloudBlobClient blobClient = cloudStorageAccount.createCloudBlobClient();
    CloudBlobContainer container = blobClient.getContainerReference(containerName);

    // At this point need to throw an error back since container itself did not exist.
    if (!container.exists()) {
        throw new Exception("WAStorageClient: generateSASURL: Container " + containerName
                + " does not exist in storage account " + storageAccountName);
    }

    SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy();
    GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
    calendar.setTime(new Date());

    //policy.setSharedAccessStartTime(calendar.getTime());
    calendar.add(Calendar.HOUR, 1);
    policy.setSharedAccessExpiryTime(calendar.getTime());
    policy.setPermissions(EnumSet.of(SharedAccessBlobPermissions.READ));

    BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
    containerPermissions.getSharedAccessPolicies().put("jenkins" + System.currentTimeMillis(), policy);
    container.uploadPermissions(containerPermissions);

    // Create a shared access signature for the container.
    String sas = container.generateSharedAccessSignature(policy, null);

    return sas;
}

From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.DataTypeUtils.java

/**
 * @param pid//from w  w w .j  av  a  2  s. co  m
 * @param pidNumber pass in the # of this pid for the sequence, i.e. normally it's 1 if you only have 1 pid entry. if this is a list of pids, then the first one is 1, second is 2 etc..
 * @param demographic
 * @throws HL7Exception
 */
public static void fillPid(PID pid, int pidNumber, Demographic demographic) throws HL7Exception {
    // defined as first pid=1 second pid=2 etc
    pid.getSetIDPID().setValue(String.valueOf(pidNumber));

    CX cx = pid.getPatientIdentifierList(0);
    // health card string, excluding version code
    cx.getIDNumber().setValue(demographic.getHin());
    cx.getIdentifierTypeCode().setValue(HEALTH_NUMBER);
    // blank for everyone but ontario use version code
    cx.getIdentifierCheckDigit().setValue(demographic.getVer());
    // province
    cx.getAssigningJurisdiction().getIdentifier().setValue(demographic.getHcType());

    GregorianCalendar tempCalendar = new GregorianCalendar();
    if (demographic.getEffDate() != null) {
        tempCalendar.setTime(demographic.getEffDate());
        cx.getEffectiveDate().setYearMonthDayPrecision(tempCalendar.get(GregorianCalendar.YEAR),
                tempCalendar.get(GregorianCalendar.MONTH) + 1,
                tempCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }

    if (demographic.getHcRenewDate() != null) {
        tempCalendar.setTime(demographic.getHcRenewDate());
        cx.getExpirationDate().setYearMonthDayPrecision(tempCalendar.get(GregorianCalendar.YEAR),
                tempCalendar.get(GregorianCalendar.MONTH) + 1,
                tempCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }

    XPN xpn = pid.getPatientName(0);
    xpn.getFamilyName().getSurname().setValue(demographic.getLastName());
    xpn.getGivenName().setValue(demographic.getFirstName());
    // Value Description
    // -----------------
    // A Alias Name
    // B Name at Birth
    // C Adopted Name
    // D Display Name
    // I Licensing Name
    // L Legal Name
    // M Maiden Name
    // N Nickname /Call me Name/Street Name
    // P Name of Partner/Spouse - obsolete (DO NOT USE)
    // R Registered Name (animals only)
    // S Coded Pseudo-Name to ensure anonymity
    // T Indigenous/Tribal/Community Name
    // U Unspecified
    xpn.getNameTypeCode().setValue("L");

    if (demographic.getBirthDay() != null) {
        DTM bday = pid.getDateTimeOfBirth();
        tempCalendar = demographic.getBirthDay();
        bday.setDatePrecision(tempCalendar.get(GregorianCalendar.YEAR),
                tempCalendar.get(GregorianCalendar.MONTH) + 1,
                tempCalendar.get(GregorianCalendar.DAY_OF_MONTH));
    }

    // Value Description
    // -----------------
    // F Female
    // M Male
    // O Other
    // U Unknown
    // A Ambiguous
    // N Not applicable
    pid.getAdministrativeSex().setValue(getHl7GenderFromOscarGender(demographic.getSex()));

    XAD address = pid.getPatientAddress(0);
    fillXAD(address, demographic, null, "H");

    XTN phone = pid.getPhoneNumberHome(0);
    phone.getUnformattedTelephoneNumber().setValue(demographic.getPhone());

    // ISO 639, hrmmm shall we say 639-1 (2 digit)?
    pid.getPrimaryLanguage().getIdentifier().setValue(demographic.getSpokenLanguage());

    // ISO table 3166.
    pid.getCitizenship(0).getIdentifier().setValue(demographic.getCitizenship());
}

From source file:org.nuclos.common2.SeriesUtils.java

/**
 * /*from w  ww.  j av  a2 s .  co  m*/
 * @param series
 * @param dateOrigin
 * @return the next date calculated by series from origin. 
 *          origin could be a calculated date (result >= origin)
 */
public static DateTime getSeriesNext(String series, DateTime dateOrigin) {
    if (series == null)
        return dateOrigin;

    GregorianCalendar calendar = new GregorianCalendar();
    calendar.setTime(dateOrigin.getDate());

    String[] split = org.apache.commons.lang.StringUtils.split(series, '|');

    if (split.length > 0) {
        String modus = split[0];

        if ("d".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            int days = Integer.parseInt(split[3]);
            if (days == 0) {
                // add one day if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }

                while (!isWorkingDay(calendar.get(GregorianCalendar.DAY_OF_WEEK))) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }

            } else {
                // add one day if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }

                if (days > 1) {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, days - 1);
                }
            }

        } else if ("w".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            int weeks = Integer.parseInt(split[3]);

            List<Integer> possibleWeekdays = new ArrayList<Integer>();
            int firstSelectedWeekday = -1000;
            int lastWeekday = -1000;

            // use getWeekdayItems() in order to get the right start (end) of the week
            for (SeriesListItem sli : getWeekdayItems()) {
                boolean addWeekday = false;

                switch (sli.getId()) {
                case GregorianCalendar.MONDAY:
                    if (split[4].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.TUESDAY:
                    if (split[5].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.WEDNESDAY:
                    if (split[6].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.THURSDAY:
                    if (split[7].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.FRIDAY:
                    if (split[8].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.SATURDAY:
                    if (split[9].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                case GregorianCalendar.SUNDAY:
                    if (split[10].equals("0") ? false : true)
                        addWeekday = true;
                    break;
                }

                if (addWeekday) {
                    possibleWeekdays.add(sli.getId());
                    if (firstSelectedWeekday == -1000)
                        firstSelectedWeekday = sli.getId();
                }

                lastWeekday = sli.getId();
            }

            // add one day if calculated date is before origin
            boolean weeksAdded = false;
            if (calendar.getTime().before(dateOrigin.getDate())) {
                if (lastWeekday == calendar.get(GregorianCalendar.DAY_OF_WEEK)) {
                    calendar.add(GregorianCalendar.WEEK_OF_YEAR, weeks - 1);
                    weeksAdded = true;
                } else {
                    calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
                }
            }

            while (!possibleWeekdays.contains(new Integer(calendar.get(GregorianCalendar.DAY_OF_WEEK)))) {
                if (!weeksAdded && lastWeekday == calendar.get(GregorianCalendar.DAY_OF_WEEK)) {
                    calendar.add(GregorianCalendar.WEEK_OF_YEAR, weeks - 1);
                }
                calendar.add(GregorianCalendar.DAY_OF_MONTH, 1);
            }

        } else if ("m".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            if ("m1".equals(split[3])) {
                int day = Integer.parseInt(split[4]);
                int months = Integer.parseInt(split[5]);

                calendar.set(GregorianCalendar.DAY_OF_MONTH, day);

                // add one month if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.MONTH, 1);
                    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
                }

                if (months > 1) {
                    calendar.add(GregorianCalendar.MONTH, months - 1);
                    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
                }

            } else {
                int number = Integer.parseInt(split[4]);
                int weekday = Integer.parseInt(split[5]);
                int months = Integer.parseInt(split[6]);

                calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);

                // add one month if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.MONTH, 1);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);
                }

                if (months > 1) {
                    calendar.add(GregorianCalendar.MONTH, months - 1);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);
                }
            }

        } else if ("y".equals(modus)) {
            int hour = Integer.parseInt(split[1]);
            int minute = Integer.parseInt(split[2]);

            calendar.set(GregorianCalendar.HOUR_OF_DAY, hour);
            calendar.set(GregorianCalendar.MINUTE, minute);

            if ("y1".equals(split[3])) {
                int day = Integer.parseInt(split[4]);
                int month = Integer.parseInt(split[5]);

                calendar.set(GregorianCalendar.MONTH, month);
                calendar.set(GregorianCalendar.DAY_OF_MONTH, day);

                // add one year if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.YEAR, 1);
                    calendar.set(GregorianCalendar.MONTH, month);
                    calendar.set(GregorianCalendar.DAY_OF_MONTH, day);
                }

            } else {
                int number = Integer.parseInt(split[4]);
                int weekday = Integer.parseInt(split[5]);
                int month = Integer.parseInt(split[6]);

                calendar.set(GregorianCalendar.MONTH, month);
                calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);

                // add one year if calculated date is before origin
                if (calendar.getTime().before(dateOrigin.getDate())) {
                    calendar.add(GregorianCalendar.YEAR, 1);
                    calendar.set(GregorianCalendar.MONTH, month);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK, weekday);
                    calendar.set(GregorianCalendar.DAY_OF_WEEK_IN_MONTH, number);
                }
            }
        }
    }

    return new DateTime(calendar.getTimeInMillis());
}

From source file:oscar.util.DateUtils.java

/**
 * @param locale can be null/*  w  ww .  j  a  va 2  s  .c o m*/
 * @return if String is null will return null.
 * @throws ParseException 
 */
public static synchronized GregorianCalendar parseDateAsCalendar(String s, Locale locale)
        throws ParseException {
    Date d = parseDate(s, locale);

    if (d == null)
        return (null);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(d);
    zeroTimeFields(cal);
    return (cal);
}

From source file:oscar.util.DateUtils.java

/**
 * This will take 2 date objects, presumably one that holds the date and the other that holds the time
 * and it will merge the two into one object that has both the date and the time. This method is not 
 * normally useful but our database seems to have a lot of split date/time objects.
 * If either parameters are null it will return null.
 * This method will materialise the result before returning.
 *///from www  . j av a2s . c  o  m
public static GregorianCalendar toGregorianCalendar(Date date, Date time) {
    if (date == null || time == null)
        return (null);

    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);

    GregorianCalendar cal2 = new GregorianCalendar();
    cal2.setTime(time);

    cal.set(GregorianCalendar.HOUR_OF_DAY, cal2.get(GregorianCalendar.HOUR_OF_DAY));
    cal.set(GregorianCalendar.MINUTE, cal2.get(GregorianCalendar.MINUTE));
    cal.set(GregorianCalendar.SECOND, cal2.get(GregorianCalendar.SECOND));
    cal.set(GregorianCalendar.MILLISECOND, cal2.get(GregorianCalendar.MILLISECOND));
    cal.getTime();

    return (cal);
}

From source file:oscar.util.DateUtils.java

/**
 * null safe method for converting a date object to calendar object.
 */// www  .j  a va  2 s  .c  o  m
public static GregorianCalendar toGregorianCalendar(Date date) {
    if (date == null)
        return (null);

    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    gregorianCalendar.setTime(date);
    gregorianCalendar.getTime();
    return (gregorianCalendar);
}

From source file:org.squale.welcom.outils.DateUtil.java

/**
 * @param date date en entre/*from  w ww.  j  ava2  s  .c  o  m*/
 * @return retourne le jour de la semaine correspondant  la date en entre
 */
public static int getDay(final java.util.Date date) {
    final GregorianCalendar gc = new GregorianCalendar();
    gc.setTime(date);

    return gc.get(Calendar.DAY_OF_WEEK);
}