Example usage for org.joda.time.format DateTimeFormatter withZoneUTC

List of usage examples for org.joda.time.format DateTimeFormatter withZoneUTC

Introduction

In this page you can find the example usage for org.joda.time.format DateTimeFormatter withZoneUTC.

Prototype

public DateTimeFormatter withZoneUTC() 

Source Link

Document

Returns a new formatter that will use the UTC zone in preference to the zone of the printed object, or default zone on a parse.

Usage

From source file:com.flooose.gpxkeeper.GPXFile.java

License:Open Source License

public void saveTrackingPointAsJSON() {
    DateTime timestamp = null;/*from   w  w  w. j a va 2 s  .co m*/

    JSONObject trackingPoint = new JSONObject();

    for (int i = 0; i < parser.getAttributeCount(); i++) {
        try {
            if ("lon".equals(parser.getAttributeName(i))) {
                trackingPoint.put(LONGITUDE, parser.getAttributeValue(i));

            } else if ("lat".equals(parser.getAttributeName(i))) {
                trackingPoint.put(LATITUDE, parser.getAttributeValue(i));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    try {
        while (!trackPointEnd()) {
            if ("ele".equals(parser.getName()) && parser.getEventType() != parser.END_TAG) {
                while (parser.getEventType() != parser.TEXT)
                    parser.next();
                trackingPoint.put(GPXFile.ELEVATION, parser.getText());
            } else if ("time".equals(parser.getName()) && parser.getEventType() != parser.END_TAG) {
                while (parser.getEventType() != parser.TEXT)
                    parser.next();
                DateTimeFormatter dtf = DateTimeFormat.forPattern(GPX_DATE_FORMAT_STRING);
                timestamp = dtf.withZoneUTC().parseDateTime(parser.getText());

                if (startTime == null) {
                    startTime = timestamp.withZone(DateTimeZone.getDefault());

                    gpsActivity.put(GPXFile.START_TIME,
                            new SimpleDateFormat(DATE_FORMAT_STRING).format(startTime.getMillis()));
                }
            }
            parser.next();
        }
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        trackingPoint.put(GPXFile.TIMESTAMP, (timestamp.getMillis() - startTime.getMillis()) / 1000); // there should be a constant defined somewhere. Find it.
        trackingPoint.put(GPXFile.TRACKING_POINT_TYPE, "gps");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    gpsPath.put(trackingPoint);
}

From source file:com.github.pockethub.android.util.TimeUtils.java

License:Apache License

/**
 * Convert string datetime in UTC to local datetime.
 * @param value The datetime in UTC to parse.
 * @return Local datetime.//from  w  w  w .  j  a  va2  s . c  o m
 */
public static Date stringToDate(String value) {
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DateTime t = format.withZoneUTC().parseDateTime(value);
    return t.toDate();
}

From source file:com.kana.connect.server.receiver.SMSKeywordDispatchReplyHandler.java

License:Apache License

/**
 * Helper method that transforms an SMPP Message into an XML document
 *///from w ww.  j av  a 2  s  .c o m
public static String smppToXml(SmppReceiverMessage msg) {
    SMPPRequest smppReq = msg.getSmppRequest();

    StringBuffer buf = new StringBuffer();
    buf.append("<smpp>\n");

    // smpp header 
    // numeric values do not need to be escaped
    buf.append("<header>");
    buf.append("<command_id>").append(smppReq.getCommandId()).append("</command_id>");
    buf.append("<sequence_number>").append(smppReq.getSequenceNum()).append("</sequence_number>");
    buf.append("</header>\n");

    // smpp source
    Address smppSource = smppReq.getSource();
    buf.append("<source>");
    buf.append("<ton>").append(smppSource.getTON()).append("</ton>");
    buf.append("<npi>").append(smppSource.getNPI()).append("</npi>");

    String srcAddr = smppSource.getAddress();
    srcAddr = StringEscapeUtils.escapeXml11(srcAddr);
    buf.append("<address>").append(srcAddr).append("</address>");
    buf.append("</source>\n");

    // smpp dest
    Address smppDest = smppReq.getDestination();
    buf.append("<destination>");
    buf.append("<ton>").append(smppDest.getTON()).append("</ton>");
    buf.append("<npi>").append(smppDest.getNPI()).append("</npi>");

    String dstAddr = smppDest.getAddress();
    dstAddr = StringEscapeUtils.escapeXml11(dstAddr);
    buf.append("<address>").append(dstAddr).append("</address>");
    buf.append("</destination>\n");

    // message id
    String msgId = smppReq.getMessageId();
    if (msgId == null) {
        msgId = "";
    }
    msgId = StringEscapeUtils.escapeXml11(msgId);
    buf.append("<messageid>").append(msgId).append("</messageid>\n");

    // smpp message
    String msgText = smppReq.getMessageText();
    msgText = StringEscapeUtils.escapeXml11(msgText);
    buf.append("<message>");
    buf.append(msgText);
    buf.append("</message>\n");

    // base64 message
    msgText = smppReq.getMessageText();
    msgText = Base64.encodeBase64String(msgText.getBytes());
    buf.append("<messageBase64>");
    buf.append(msgText);
    buf.append("</messageBase64>\n");

    //
    // timestamps in different formats
    //

    long now = System.currentTimeMillis();

    // ISO8601 http://joda-time.sourceforge.net/apidocs/org/joda/time/format/ISODateTimeFormat.html
    DateTimeFormatter isoFormat = ISODateTimeFormat.dateTime();
    isoFormat.withZoneUTC();
    String headerTimestamp = isoFormat.print(now);
    headerTimestamp = StringEscapeUtils.escapeXml11(headerTimestamp);

    // custom format http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html
    DateTimeFormatter otherFormat = DateTimeFormat.forPattern("ddMMYYYYHHmmz");
    String payloadTimestamp = otherFormat.print(now);
    payloadTimestamp = StringEscapeUtils.escapeXml11(payloadTimestamp);

    buf.append("<headertimestamp>").append(headerTimestamp).append("</headertimestamp>");
    buf.append("<payloadtimestamp>").append(payloadTimestamp).append("</payloadtimestamp>");

    buf.append("</smpp>\n");
    return buf.toString();
}

From source file:com.meisolsson.githubsdk.core.FormattedTimeAdapter.java

License:Apache License

@FromJson
@FormattedTime// w w  w .  jav a2 s .  c om
Date fromJson(String time) {
    DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
    DateTime t = format.withZoneUTC().parseDateTime(time);
    return t.toDate();
}

From source file:com.thinkbiganalytics.policy.standardization.DateTimeStandardizer.java

License:Apache License

/**
 * Returns a time formatter for the specified timezone
 *
 * @param format   the current formatter
 * @param timezone the timezone string/*from  www.  ja v  a  2 s.  co m*/
 * @return a time formatter for the specified timezone
 */
protected DateTimeFormatter formatterForTimezone(DateTimeFormatter format, String timezone) {

    if (StringUtils.isEmpty(timezone)) {
        return format;
    }
    if ("UTC".equals(timezone)) {
        return format.withZoneUTC();
    }
    return format.withZone(DateTimeZone.forTimeZone(TimeZone.getTimeZone(timezone)));

}

From source file:eu.europa.ec.fisheries.uvms.common.DateUtils.java

License:Open Source License

public static Date stringToDate(String dateString) throws IllegalArgumentException {
    if (dateString != null) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(FORMAT).withOffsetParsed();
        DateTime dateTime = formatter.withZoneUTC().parseDateTime(dateString);
        GregorianCalendar cal = dateTime.toGregorianCalendar();
        return cal.getTime();
    } else {//w  w w .  j a v  a 2s  . c o m
        return null;
    }
}

From source file:eu.europa.ec.fisheries.uvms.exchange.model.util.DateUtils.java

License:Open Source License

private static Date parseToUTC(String format, String dateString) {
    DateTimeFormatter formatter = DateTimeFormat.forPattern(format).withOffsetParsed();
    DateTime dateTime = formatter.withZoneUTC().parseDateTime(dateString);
    GregorianCalendar cal = dateTime.toGregorianCalendar();
    return cal.getTime();
}

From source file:eu.europa.ec.fisheries.uvms.movement.util.DateUtil.java

License:Open Source License

public static Date parseToUTCDate(String dateTimeInUTC) {
    for (DateFormats format : DateFormats.values()) {
        DateTimeFormatter formatter = DateTimeFormat.forPattern(format.getFormat());
        DateTime dateTime = formatter.withZoneUTC().parseDateTime(dateTimeInUTC);
        if (dateTime != null) {
            return dateTime.toLocalDateTime().toDate();
        }//from w  w  w.j  a v  a  2 s .  c  om
    }
    LOG.error("Could not parse dateTimeInUTC: " + dateTimeInUTC + " with pattern any defined pattern.");
    return null;
}

From source file:eu.europa.ec.fisheries.uvms.movement.util.DateUtil.java

License:Open Source License

public static Date parseToUTCDate_a_more_stable_if_you_want_to_work_like_this(String dateTimeInUTC) {
    try {//  w ww.  j a v a  2s  .  c  o  m
        for (DateFormats format : DateFormats.values()) {
            try {
                DateTimeFormatter formatter = DateTimeFormat.forPattern(format.getFormat());
                DateTime dateTime = formatter.withZoneUTC().parseDateTime(dateTimeInUTC);
                if (dateTime != null) {
                    return dateTime.toLocalDateTime().toDate();
                }
            } catch (RuntimeException e) {
                LOG.error("Could not parse dateTimeInUTC: " + dateTimeInUTC
                        + " with pattern any defined pattern.");
                continue;
            }
        }
    } catch (RuntimeException e) {
        LOG.error("Could not parse dateTimeInUTC: " + dateTimeInUTC + " with pattern any defined pattern.");
        return null;
    }
    LOG.error("Could not parse dateTimeInUTC: " + dateTimeInUTC + " with pattern any defined pattern.");
    return null;
}

From source file:eu.europa.ec.fisheries.uvms.plugins.flux.util.DateUtil.java

License:Open Source License

public static Date parseToUTCDate(String dateString) throws IllegalArgumentException {
    try {/*from w w  w.  j a  v  a2s .  co  m*/
        if (dateString != null) {
            DateTimeFormatter formatter = DateTimeFormat.forPattern(FORMAT).withOffsetParsed();
            DateTime dateTime = formatter.withZoneUTC().parseDateTime(dateString);
            GregorianCalendar cal = dateTime.toGregorianCalendar();
            return cal.getTime();
        } else {
            return null;
        }
    } catch (IllegalArgumentException e) {
        LOG.error(e.getMessage());
        throw new IllegalArgumentException(e);
    }
}