Example usage for org.joda.time DateTime toGregorianCalendar

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

Introduction

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

Prototype

public GregorianCalendar toGregorianCalendar() 

Source Link

Document

Get the date time as a java.util.GregorianCalendar, assigning exactly the same millisecond instant.

Usage

From source file:com.helger.datetime.xml.PDTXMLConverter.java

License:Apache License

/**
 * Get the passed object as {@link XMLGregorianCalendar} with date and time.
 *
 * @param aBase/*from   w ww. ja  v  a  2  s .  c  om*/
 *        The source object. May be <code>null</code>.
 * @return <code>null</code> if the parameter is <code>null</code>.
 */
@Nullable
public static XMLGregorianCalendar getXMLCalendar(@Nullable final DateTime aBase) {
    return aBase == null ? null : s_aDTFactory.newXMLGregorianCalendar(aBase.toGregorianCalendar());
}

From source file:com.telefonica.iot.cygnus.utils.CommonUtils.java

License:Open Source License

/**
 * Gets the timestamp within a TimeInstant metadata, if exists.
 * @param metadata/*  w ww . j a  va  2s.c  o m*/
 * @return The timestamp within a TimeInstant metadata
 */
public static Long getTimeInstant(String metadata) {
    Long res = null;
    JSONParser parser = new JSONParser();
    JSONArray mds;

    try {
        mds = (JSONArray) parser.parse(metadata);
    } catch (ParseException e) {
        LOGGER.error("Error while parsing the metadaga. Details: " + e.getMessage());
        return null;
    } // try catch

    for (Object mdObject : mds) {
        JSONObject md = (JSONObject) mdObject;
        String mdName = (String) md.get("name");

        if (mdName.equals("TimeInstant")) {
            String mdValue = (String) md.get("value");

            if (isANumber(mdValue)) {
                res = new Long(mdValue);
            } else {
                DateTime dateTime;

                try {
                    // ISO 8601 without miliseconds
                    dateTime = FORMATTER1.parseDateTime(mdValue);
                } catch (Exception e1) {
                    LOGGER.debug(e1.getMessage());

                    try {
                        // ISO 8601 with miliseconds
                        dateTime = FORMATTER2.parseDateTime(mdValue);
                    } catch (Exception e2) {
                        LOGGER.debug(e2.getMessage());

                        try {
                            // ISO 8601 with microsencods
                            String mdValueTruncated = mdValue.substring(0, mdValue.length() - 4) + "Z";
                            dateTime = FORMATTER2.parseDateTime(mdValueTruncated);
                        } catch (Exception e3) {
                            LOGGER.debug(e3.getMessage());

                            try {
                                // SQL timestamp without miliseconds
                                dateTime = FORMATTER3.parseDateTime(mdValue);
                            } catch (Exception e4) {
                                LOGGER.debug(e4.getMessage());

                                try {
                                    // SQL timestamp with miliseconds
                                    dateTime = FORMATTER4.parseDateTime(mdValue);
                                } catch (Exception e5) {
                                    LOGGER.debug(e5.getMessage());

                                    try {
                                        // SQL timestamp with microseconds
                                        String mdValueTruncated = mdValue.substring(0, mdValue.length() - 3);
                                        dateTime = FORMATTER4.parseDateTime(mdValueTruncated);
                                    } catch (Exception e6) {
                                        LOGGER.debug(e6.getMessage());
                                        return null;
                                    } // try catch
                                } // try catch
                            } // try catch
                        } // try catch
                    } // try catch
                } // try catch

                GregorianCalendar cal = dateTime.toGregorianCalendar();
                res = cal.getTimeInMillis();
            } // if else

            break;
        } // if
    } // for

    return res;
}

From source file:com.thoughtworks.xstream.converters.extended.ISO8601GregorianCalendarConverter.java

License:Open Source License

public Object fromString(String str) {
    for (int i = 0; i < formattersUTC.length; i++) {
        DateTimeFormatter formatter = formattersUTC[i];
        try {/*w  w  w.  ja  v  a 2  s.com*/
            DateTime dt = formatter.parseDateTime(str);
            Calendar calendar = dt.toGregorianCalendar();
            calendar.setTimeZone(TimeZone.getDefault());
            return calendar;
        } catch (IllegalArgumentException e) {
            // try with next formatter
        }
    }
    String timeZoneID = TimeZone.getDefault().getID();
    for (int i = 0; i < formattersNoUTC.length; i++) {
        try {
            DateTimeFormatter formatter = formattersNoUTC[i].withZone(DateTimeZone.forID(timeZoneID));
            DateTime dt = formatter.parseDateTime(str);
            Calendar calendar = dt.toGregorianCalendar();
            calendar.setTimeZone(TimeZone.getDefault());
            return calendar;
        } catch (IllegalArgumentException e) {
            // try with next formatter
        }
    }
    throw new ConversionException("Cannot parse date " + str);
}

From source file:com.thoughtworks.xstream.core.util.ISO8601JodaTimeConverter.java

License:Open Source License

@Override
public Object fromString(final String str) {
    for (final DateTimeFormatter formatter : formattersUTC) {
        try {//from   ww w. jav  a2s  .c  om
            final DateTime dt = formatter.parseDateTime(str);
            final Calendar calendar = dt.toGregorianCalendar();
            calendar.setTimeZone(TimeZone.getDefault());
            return calendar;
        } catch (final IllegalArgumentException e) {
            // try with next formatter
        }
    }
    final DateTimeZone dateTimeZone = DateTimeZone.forTimeZone(TimeZone.getDefault());
    for (final DateTimeFormatter element : formattersNoUTC) {
        try {
            final DateTimeFormatter formatter = element.withZone(dateTimeZone);
            final DateTime dt = formatter.parseDateTime(str);
            final Calendar calendar = dt.toGregorianCalendar();
            calendar.setTimeZone(TimeZone.getDefault());
            return calendar;
        } catch (final IllegalArgumentException e) {
            // try with next formatter
        }
    }
    final ConversionException exception = new ConversionException("Cannot parse date");
    exception.add("date", str);
    throw exception;
}

From source file:de.azapps.mirakel.model.semantic.Semantic.java

License:Open Source License

public void apply(@NonNull final Task task) {
    if (getPriority().isPresent()) {
        task.setPriority(getPriority().get());
    }/*from   w  w w  .ja  v  a  2  s  . c  o  m*/
    if (getDue().isPresent()) {
        final DateTime due = getNormalizedCalendar();
        due.plusDays(getDue().get());
        task.setDue(of(due));
    }
    if (getList().isPresent()) {
        task.setList(getList().get());
    }
    if (getWeekday().isPresent()) {
        final DateTime due = getNormalizedCalendar();
        int nextWeekday = getWeekday().get() + 1;
        // Because there are some dudes which means, sunday is the
        // first day of the week That's obviously wrong!
        final Calendar cal = due.toGregorianCalendar();
        if (nextWeekday == 8) {
            nextWeekday = 1;
        }
        do {
            cal.add(Calendar.DAY_OF_YEAR, 1);
        } while (cal.get(Calendar.DAY_OF_WEEK) != nextWeekday);
        task.setDue(of(new DateTime(cal)));
    }
}

From source file:de.uniluebeck.itm.spyglass.gui.wizard.WisebedPacketReaderConfigurationWizard.java

License:Open Source License

private void fillReservations() {

    final RS rs = RSServiceHelper.getRSService(config.getRsEndpointUrl());
    final GetReservations getReservationsRequest = new GetReservations();
    final DateTime from = new DateTime();
    final DateTime until = from.plusHours(24);

    getReservationsRequest.setFrom(datatypeFactory.newXMLGregorianCalendar(from.toGregorianCalendar()));
    getReservationsRequest.setTo(datatypeFactory.newXMLGregorianCalendar(until.toGregorianCalendar()));

    List<eu.wisebed.api.rs.SecretAuthenticationKey> secretAuthenticationKeys = BeanShellHelper
            .copySnaaToRs(Lists.<SecretAuthenticationKey>newArrayList(authMap.values()));

    final List<ConfidentialReservationData> rsData;
    try {//from   w  w w  . j  av  a  2  s .c  o  m
        rsData = rs.getConfidentialReservations(secretAuthenticationKeys, getReservationsRequest);
    } catch (RSExceptionException e) {
        throw new RuntimeException(e);
    }

    selectReservationPage.clearReservations();

    for (ConfidentialReservationData data : rsData) {
        selectReservationPage.addReservation(data);
    }

    if (selectReservationPage.reservationsTable.getItemCount() > 0) {
        selectReservationPage.reservationsTable.select(0);
    }
}

From source file:de.zib.gndms.dspace.slice.client.SliceClient.java

License:Apache License

public Calendar getTerminationTime() throws Exception, ParseException, DeserializationException {
    GetResourcePropertyResponse resp = getResourceProperty(SliceConstants.TERMINATIONTIME);
    //Calendar tt = (Calendar) ObjectDeserializer.toObject( resp.get_any()[0], GregorianCalendar.class );
    String tm = (String) resp.get_any()[0].getObjectValue(String.class);
    DateTime dt = new DateTime(tm);
    return dt.toGregorianCalendar();
}

From source file:de.zib.gndms.gritserv.typecon.types.ContractXSDTypeWriter.java

License:Apache License

public void writeIfDecisionBefore(DateTime dat) {
    getProduct().setIfDecisionBefore(dat.toGregorianCalendar());
}

From source file:de.zib.gndms.model.common.types.TransientContract.java

License:Apache License

/**
 * Creates a persistent-contract form this contract by fixing future time using the accepted time stamp.
 *
 * @note The created contract may be invalid wrt its jpa constraints.
 *
 * @return A persistent-contract representing this contract.
 *
 * todo maybe set fixed values here//from  w ww  .j av  a2s  .com
 */
@SuppressWarnings({ "FeatureEnvy" })
public PersistentContract acceptAsIs() {

    PersistentContract pc = new PersistentContract();
    pc.setAccepted(accepted.toGregorianCalendar());

    DateTime fixedDeadline = null;
    if (hasDeadline()) {
        fixedDeadline = getDeadline().fixedWith(accepted).getFixedTime();
        pc.setDeadline(fixedDeadline.toGregorianCalendar());
    }

    if (hasResultValidity()) {
        DateTime fixedResultValidity;
        if (fixedDeadline != null)
            fixedResultValidity = getResultValidity().fixedWith(fixedDeadline).getFixedTime();
        else {
            fixedResultValidity = getResultValidity().fixedWith(accepted).getFixedTime();
        }
        pc.setResultValidity(fixedResultValidity.toGregorianCalendar());
    }

    if (hasExpectedSize())
        pc.setExpectedSize(getExpectedSize());
    return pc;
}

From source file:de.zib.gndms.model.util.JodaTimeForJPA.java

License:Apache License

public static Calendar fromDateTime(final DateTime dateTime) {
    return dateTime.toGregorianCalendar();
}