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

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

Introduction

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

Prototype

public DateTimeFormatter withOffsetParsed() 

Source Link

Document

Returns a new formatter that will create a datetime with a time zone equal to that of the offset of the parsed string.

Usage

From source file:com.sos.hibernate.classes.UtcTimeHelper.java

License:Apache License

public static String convertTimeZonesToString(String dateFormat, String fromTimeZone, String toTimeZone,
        DateTime fromDateTime) {/*from   w w w . jav  a2 s.c o m*/
    DateTimeZone fromZone = DateTimeZone.forID(fromTimeZone);
    DateTimeZone toZone = DateTimeZone.forID(toTimeZone);

    DateTime dateTime = new DateTime(fromDateTime);

    dateTime = dateTime.withZoneRetainFields(fromZone);

    DateTime toDateTime = new DateTime(dateTime).withZone(toZone);

    DateTimeFormatter oFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'H:mm:ss.SSSZ");
    DateTimeFormatter oFormatter2 = DateTimeFormat.forPattern(dateFormat);

    DateTime newDate = oFormatter.withOffsetParsed().parseDateTime(toDateTime.toString());

    return oFormatter2.withZone(toZone).print(newDate.getMillis());

}

From source file:com.sos.hibernate.classes.UtcTimeHelper.java

License:Apache License

public static Date convertTimeZonesToDate(String fromTimeZone, String toTimeZone, DateTime fromDateTime) {
    if (fromDateTime == null) {
        return null;
    }/*  ww  w.ja va2  s .  c om*/
    DateTimeZone fromZone = DateTimeZone.forID(fromTimeZone);
    DateTimeZone toZone = DateTimeZone.forID(toTimeZone);

    DateTime dateTime = new DateTime(fromDateTime);

    dateTime = dateTime.withZoneRetainFields(fromZone);

    DateTime toDateTime = new DateTime(dateTime).withZone(toZone);

    DateTimeFormatter oFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'H:mm:ss.SSSZ");
    DateTimeFormatter oFormatter2 = DateTimeFormat.forPattern("yyyy-MM-dd H:mm:ss.ss");

    DateTime newDate = oFormatter.withOffsetParsed().parseDateTime(toDateTime.toString());

    try {
        return new SimpleDateFormat("yyyy-MM-dd H:mm:ss.ss")
                .parse(oFormatter2.withZone(toZone).print(newDate.getMillis()));
    } catch (ParseException e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.splicemachine.derby.utils.SpliceDateFunctions.java

License:Apache License

private static long parseDateTime(String source, String format) throws SQLException {
    // FIXME: Timezone loss for Timestamp - see http://stackoverflow.com/questions/16794772/joda-time-parse-a-date-with-timezone-and-retain-that-timezone
    DateTimeFormatter parser = DEFAULT_DATE_TIME_FORMATTER;
    if (format != null) {
        try {/*  w w w. j  a v  a  2s .  c  o m*/
            parser = DateTimeFormat.forPattern(format);
        } catch (Exception e) {
            throw new SQLException("Error creating a datetime parser for pattern: " + format + ". Try using an"
                    + " ISO8601 pattern such as, yyyy-MM-dd'T'HH:mm:ss.SSSZZ, yyyy-MM-dd'T'HH:mm:ssZ or yyyy-MM-dd",
                    SQLState.LANG_DATE_SYNTAX_EXCEPTION);
        }
    }
    DateTime parsed;
    try {
        parsed = parser.withOffsetParsed().parseDateTime(source);
    } catch (Exception e) {
        throw new SQLException("Error parsing datetime " + source + " with pattern: " + format
                + ". Try using an"
                + " ISO8601 pattern such as, yyyy-MM-dd'T'HH:mm:ss.SSSZZ, yyyy-MM-dd'T'HH:mm:ssZ or yyyy-MM-dd",
                SQLState.LANG_DATE_SYNTAX_EXCEPTION);
    }
    return parsed.getMillis();
}

From source file:org.apache.pig.builtin.JsonLoader.java

License:Apache License

private Object readField(JsonParser p, ResourceFieldSchema field, int fieldnum) throws IOException {
    // Read the next token
    JsonToken tok = p.nextToken();/*from  w w w .j av a 2 s. c  o m*/
    if (tok == null) {
        warn("Early termination of record, expected " + schema.getFields().length + " fields bug found "
                + fieldnum, PigWarning.UDF_WARNING_1);
        return null;
    }

    // Check to see if this value was null
    if (tok == JsonToken.VALUE_NULL)
        return null;

    // Read based on our expected type
    switch (field.getType()) {
    case DataType.BOOLEAN:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getBooleanValue();

    case DataType.INTEGER:
        // Read the field name
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getIntValue();

    case DataType.LONG:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getLongValue();

    case DataType.FLOAT:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getFloatValue();

    case DataType.DOUBLE:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getDoubleValue();

    case DataType.DATETIME:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        DateTimeFormatter formatter = ISODateTimeFormat.dateTimeParser();
        return formatter.withOffsetParsed().parseDateTime(p.getText());

    case DataType.BYTEARRAY:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        byte[] b = p.getText().getBytes();
        // Use the DBA constructor that copies the bytes so that we own
        // the memory
        return new DataByteArray(b, 0, b.length);

    case DataType.CHARARRAY:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getText();

    case DataType.BIGINTEGER:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getBigIntegerValue();

    case DataType.BIGDECIMAL:
        tok = p.nextToken();
        if (tok == JsonToken.VALUE_NULL)
            return null;
        return p.getDecimalValue();

    case DataType.MAP:
        // Should be a start of the map object
        if (p.nextToken() != JsonToken.START_OBJECT) {
            warn("Bad map field, could not find start of object, field " + fieldnum, PigWarning.UDF_WARNING_1);
            return null;
        }
        Map<String, String> m = new HashMap<String, String>();
        while (p.nextToken() != JsonToken.END_OBJECT) {
            String k = p.getCurrentName();
            String v = p.getCurrentToken() == JsonToken.VALUE_NULL ? null : p.getText();
            m.put(k, v);
        }
        return m;

    case DataType.TUPLE:
        if (p.nextToken() != JsonToken.START_OBJECT) {
            warn("Bad tuple field, could not find start of object, " + "field " + fieldnum,
                    PigWarning.UDF_WARNING_1);
            return null;
        }

        ResourceSchema s = field.getSchema();
        ResourceFieldSchema[] fs = s.getFields();
        Tuple t = tupleFactory.newTuple(fs.length);

        for (int j = 0; j < fs.length; j++) {
            t.set(j, readField(p, fs[j], j));
        }

        if (p.nextToken() != JsonToken.END_OBJECT) {
            warn("Bad tuple field, could not find end of object, " + "field " + fieldnum,
                    PigWarning.UDF_WARNING_1);
            return null;
        }
        return t;

    case DataType.BAG:
        if (p.nextToken() != JsonToken.START_ARRAY) {
            warn("Bad bag field, could not find start of array, " + "field " + fieldnum,
                    PigWarning.UDF_WARNING_1);
            return null;
        }

        s = field.getSchema();
        fs = s.getFields();
        // Drill down the next level to the tuple's schema.
        s = fs[0].getSchema();
        fs = s.getFields();

        DataBag bag = bagFactory.newDefaultBag();

        JsonToken innerTok;
        while ((innerTok = p.nextToken()) != JsonToken.END_ARRAY) {
            if (innerTok != JsonToken.START_OBJECT) {
                warn("Bad bag tuple field, could not find start of " + "object, field " + fieldnum,
                        PigWarning.UDF_WARNING_1);
                return null;
            }

            t = tupleFactory.newTuple(fs.length);
            for (int j = 0; j < fs.length; j++) {
                t.set(j, readField(p, fs[j], j));
            }

            if (p.nextToken() != JsonToken.END_OBJECT) {
                warn("Bad bag tuple field, could not find end of " + "object, field " + fieldnum,
                        PigWarning.UDF_WARNING_1);
                return null;
            }
            bag.add(t);
        }
        return bag;
    default:
        throw new IOException("Unknown type in input schema: " + field.getType());
    }

}

From source file:org.forgerock.openidm.util.DateUtil.java

License:CDDL license

/**
 * Parses an ISO8601 compliant timestamp into a DateTime object.
 *
 * @param timestamp//from   w  w w  . jav  a2 s .  com
 *            timestamp to parse
 * @return DateTime using the zone and chronology indicated by the timestamp
 */
public DateTime parseTimestamp(String timestamp) {
    DateTimeFormatter parser = ISODateTimeFormat.dateTime();
    return parser.withOffsetParsed().parseDateTime(timestamp);
}

From source file:org.openmainframe.ade.ext.os.parser.LinuxSyslog5424ParserBase.java

License:Open Source License

/**
 * This class converts the extracted time-stamp to a Date object by iterating
 * over the possible DateTimeFormatter objects until one is able to
 * successfully parse it./*from   w w  w.  j  a  v a  2 s .c om*/
 * @param source the source name string value.
 * @param s the date and time string value.
 * @return Date object with date/time-stamp of the Linux log.
 */
@Override
public Date toDate(String source, String s) {
    for (DateTimeFormatter fmt : dt_formatters) {
        try {
            final DateTime dt = fmt.withOffsetParsed().parseDateTime(s);
            m_dateTime = dt;
            return dt.toDate();
        } catch (IllegalArgumentException e) {
            // Ignore and continue to the next formatter.
        }
    }
    throw new IllegalArgumentException("Failed to parse date " + s);
}

From source file:uk.ac.ucl.excites.sapelli.storage.model.columns.TimeStampColumn.java

License:Apache License

protected TimeStamp parse(String value, DateTimeFormatter formatter) throws IllegalArgumentException {
    return new TimeStamp(formatter.withOffsetParsed().parseDateTime(value)); // always parse UTC offset (even when keepLocalTimezone=false, that only affects binary storage)
}