Example usage for org.joda.time.format ISODateTimeFormat dateTimeParser

List of usage examples for org.joda.time.format ISODateTimeFormat dateTimeParser

Introduction

In this page you can find the example usage for org.joda.time.format ISODateTimeFormat dateTimeParser.

Prototype

public static DateTimeFormatter dateTimeParser() 

Source Link

Document

Returns a generic ISO datetime parser which parses either a date or a time or both.

Usage

From source file:nz.net.ultraq.jaxb.adapters.XMLDateTimeAdapter.java

License:Apache License

/**
 * Converts a Joda DateTime to an XML/ISO8601 date/time string.
 * /*www.  j  a va  2 s.c  om*/
 * @param value
 * @return XML date/time string.
 */
@Override
public String marshal(DateTime value) {

    return ISODateTimeFormat.dateTimeParser().print(value);
}

From source file:nz.net.ultraq.jaxb.adapters.XMLDateTimeAdapter.java

License:Apache License

/**
 * Converts any ISO8601 date/time string into a Joda DateTime object.
 * //from   w  w  w  . ja v  a2  s. c  o  m
 * @param value
 * @return Joda DateTime.
 */
@Override
public DateTime unmarshal(String value) {

    return ISODateTimeFormat.dateTimeParser().parseDateTime(value);
}

From source file:org.activiti.engine.impl.calendar.DurationHelper.java

License:Apache License

private Calendar parseDate(String date) throws Exception {
    return ISODateTimeFormat.dateTimeParser()
            .withZone(DateTimeZone.forTimeZone(clockReader.getCurrentTimeZone())).parseDateTime(date)
            .toCalendar(null);//from  www .j  a  v  a 2 s  . co m
}

From source file:org.adl.datamodels.datatypes.DateTimeValidatorImpl.java

/**
 * Compares two valid data model elements for equality.
 * //from   w  ww.j a  v a  2s.  c  o m
 * @param iFirst  The first value being compared.
 * 
 * @param iSecond The second value being compared.
 * 
 * @param iDelimiters The common set of delimiters associated with the
 * values being compared.
 * 
 * @return Returns <code>true</code> if the two values are equal, otherwise
 *         <code>false</code>.
 */
@Override
public boolean compare(String iFirst, String iSecond, List<DMDelimiter> iDelimiters) {

    boolean equal = true;

    DateTimeFormatter dtp = ISODateTimeFormat.dateTimeParser();

    try {
        // Parse the first string and remove the sub-seconds
        DateTime dt1 = dtp.parseDateTime(iFirst);
        dt1 = new DateTime(dt1.getYear(), dt1.getMonthOfYear(), dt1.getDayOfMonth(), dt1.getHourOfDay(),
                dt1.getMinuteOfHour(), dt1.getSecondOfMinute(), 0);

        // Parse the second string and remove the sub-seconds
        DateTime dt2 = dtp.parseDateTime(iSecond);
        dt2 = new DateTime(dt2.getYear(), dt2.getMonthOfYear(), dt2.getDayOfMonth(), dt2.getHourOfDay(),
                dt2.getMinuteOfHour(), dt2.getSecondOfMinute(), 0);

        equal = dt1.equals(dt2);
    } catch (Exception e) {
        // String format error -- these cannot be equal
        equal = false;
    }

    return equal;
}

From source file:org.aksw.simba.bengal.triple2nl.converter.LiteralConverter.java

License:Apache License

private String convertDateLiteral(LiteralLabel lit) {
    RDFDatatype dt = lit.getDatatype();//from ww  w  . j  a  v  a2  s .com
    String s = lit.getLexicalForm();

    try {
        Object value = lit.getValue();
        if (value instanceof XSDDateTime) {
            Calendar calendar = ((XSDDateTime) value).asCalendar();
            if (dt.equals(XSDDatatype.XSDgMonth)) {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
            } else if (dt.equals(XSDDatatype.XSDgMonthDay)) {
                s = calendar.get(Calendar.DAY_OF_MONTH)
                        + calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL);
            } else if (dt.equals(XSDDatatype.XSDgYearMonth)) {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " "
                        + calendar.get(Calendar.YEAR);
            } else if (dt.equals(XSDDatatype.XSDgYear)) {
                s = "" + calendar.get(Calendar.YEAR);
            } else {
                s = calendar.getDisplayName(Calendar.MONTH, Calendar.LONG, ENGLISH_LOCAL) + " "
                        + calendar.get(Calendar.DAY_OF_MONTH) + ", " + calendar.get(Calendar.YEAR);
                // dateFormat.format(calendar.getTime());
            }
        }
    } catch (DatatypeFormatException | IllegalDateTimeFieldException e) {
        logger.error("Conversion of date literal " + lit + " failed. Reason: " + e.getMessage());
        // fallback
        // DateTime time = ISODateTimeFormat.dateTime
        DateTime time;
        try {
            time = ISODateTimeFormat.dateTimeParser().parseDateTime(lit.getLexicalForm());
            s = time.toString("MMMM dd, yyyy");
        } catch (Exception e1) {
            try {
                time = ISODateTimeFormat.localDateParser().parseDateTime(lit.getLexicalForm());
                s = time.toString("MMMM dd, yyyy");
            } catch (Exception e2) {
                e2.printStackTrace();
                time = ISODateTimeFormat.dateParser().parseDateTime(lit.getLexicalForm());
                s = time.toString("MMMM dd, yyyy");
            }
        }
    }
    return s;
}

From source file:org.apache.hadoop.hive.druid.serde.DruidGroupByQueryRecordReader.java

License:Apache License

@Override
public DruidWritable getCurrentValue() throws IOException, InterruptedException {
    // Create new value
    DruidWritable value = new DruidWritable();
    // 1) The timestamp column
    value.getValue().put(DruidTable.DEFAULT_TIMESTAMP_COLUMN, current.getTimestamp().getMillis());
    // 2) The dimension columns
    for (int i = 0; i < query.getDimensions().size(); i++) {
        DimensionSpec ds = query.getDimensions().get(i);
        List<String> dims = current.getDimension(ds.getOutputName());
        if (dims.size() == 0) {
            // NULL value for dimension
            value.getValue().put(ds.getOutputName(), null);
        } else {/* www  . j a v a  2 s . c o m*/
            int pos = dims.size() - indexes[i] - 1;
            Object val;
            switch (dimensionTypes[i].getPrimitiveCategory()) {
            case TIMESTAMP:
                // FLOOR extraction function
                val = ISODateTimeFormat.dateTimeParser().parseMillis((String) dims.get(pos));
                break;
            case INT:
                // EXTRACT extraction function
                val = Integer.valueOf((String) dims.get(pos));
                break;
            default:
                val = dims.get(pos);
            }
            value.getValue().put(ds.getOutputName(), val);
        }
    }
    int counter = 0;
    // 3) The aggregation columns
    for (AggregatorFactory af : query.getAggregatorSpecs()) {
        switch (extractors[counter++]) {
        case FLOAT:
            value.getValue().put(af.getName(), current.getFloatMetric(af.getName()));
            break;
        case LONG:
            value.getValue().put(af.getName(), current.getLongMetric(af.getName()));
            break;
        }
    }
    // 4) The post-aggregation columns
    for (PostAggregator pa : query.getPostAggregatorSpecs()) {
        assert extractors[counter++] == Extract.FLOAT;
        value.getValue().put(pa.getName(), current.getFloatMetric(pa.getName()));
    }
    return value;
}

From source file:org.apache.hadoop.hive.druid.serde.DruidGroupByQueryRecordReader.java

License:Apache License

@Override
public boolean next(NullWritable key, DruidWritable value) {
    if (nextKeyValue()) {
        // Update value
        value.getValue().clear();/*from  w  ww .  j a  va2  s. c  o  m*/
        // 1) The timestamp column
        value.getValue().put(DruidTable.DEFAULT_TIMESTAMP_COLUMN, current.getTimestamp().getMillis());
        // 2) The dimension columns
        for (int i = 0; i < query.getDimensions().size(); i++) {
            DimensionSpec ds = query.getDimensions().get(i);
            List<String> dims = current.getDimension(ds.getOutputName());
            if (dims.size() == 0) {
                // NULL value for dimension
                value.getValue().put(ds.getOutputName(), null);
            } else {
                int pos = dims.size() - indexes[i] - 1;
                Object val;
                switch (dimensionTypes[i].getPrimitiveCategory()) {
                case TIMESTAMP:
                    // FLOOR extraction function
                    val = ISODateTimeFormat.dateTimeParser().parseMillis((String) dims.get(pos));
                    break;
                case INT:
                    // EXTRACT extraction function
                    val = Integer.valueOf((String) dims.get(pos));
                    break;
                default:
                    val = dims.get(pos);
                }
                value.getValue().put(ds.getOutputName(), val);
            }
        }
        int counter = 0;
        // 3) The aggregation columns
        for (AggregatorFactory af : query.getAggregatorSpecs()) {
            switch (extractors[counter++]) {
            case FLOAT:
                value.getValue().put(af.getName(), current.getFloatMetric(af.getName()));
                break;
            case LONG:
                value.getValue().put(af.getName(), current.getLongMetric(af.getName()));
                break;
            }
        }
        // 4) The post-aggregation columns
        for (PostAggregator pa : query.getPostAggregatorSpecs()) {
            assert extractors[counter++] == Extract.FLOAT;
            value.getValue().put(pa.getName(), current.getFloatMetric(pa.getName()));
        }
        return true;
    }
    return false;
}

From source file:org.apache.isis.applib.fixturescripts.ExecutionParameters.java

License:Apache License

public void setParameter(final String parameterName, final DateTime parameterValue) {
    final StringBuffer buf = new StringBuffer();
    ISODateTimeFormat.dateTimeParser().printTo(buf, parameterValue);
    setParameter(parameterName, parameterValue != null ? buf.toString() : null);
}

From source file:org.apache.marmotta.commons.sesame.facading.util.FacadeUtils.java

License:Apache License

/**
 * Transform a value passed as string to the base type (i.e. non-complex type) given as argument
 *
 * @param <T>/*from  w  ww.  ja  v a2 s  .  c om*/
 * @param value
 * @param returnType
 * @return
 * @throws IllegalArgumentException
 */
@SuppressWarnings("unchecked")
public static <T> T transformToBaseType(String value, Class<T> returnType) throws IllegalArgumentException {
    // transformation to appropriate primitive type
    /*
     * README: the "dirty" cast: "(T) x" instead of "returnType.cast(x)" is required since
     * .cast does not work for primitive types (int, double, float, etc...).
     * Somehow it results in a ClassCastException
     */
    if (Integer.class.equals(returnType) || int.class.equals(returnType)) {
        if (value == null) {
            return (T) (Integer) (0);
        }
        return (T) (Integer.decode(value));
    } else if (Long.class.equals(returnType) || long.class.equals(returnType)) {
        if (value == null) {
            return (T) (Long) (0L);
        }
        return (T) (Long.decode(value));
    } else if (Double.class.equals(returnType) || double.class.equals(returnType)) {
        if (value == null) {
            return (T) (Double) (0.0);
        }
        return (T) (Double.valueOf(value));
    } else if (Float.class.equals(returnType) || float.class.equals(returnType)) {
        if (value == null) {
            return (T) (Float) (0.0F);
        }
        return (T) (Float.valueOf(value));
    } else if (Byte.class.equals(returnType) || byte.class.equals(returnType)) {
        if (value == null) {
            return (T) (Byte) ((byte) 0);
        }
        return (T) (Byte.decode(value));
    } else if (Boolean.class.equals(returnType) || boolean.class.equals(returnType)) {
        return (T) (Boolean.valueOf(value));
    } else if (Character.class.equals(returnType) || char.class.equals(returnType)) {
        if (value == null) {
            if (Character.class.equals(returnType)) {
                return null;
            } else {
                return (T) new Character((char) 0);
            }
        } else if (value.length() > 0) {
            return (T) (Character) (value.charAt(0));
        } else {
            return null;
        }
    } else if (Locale.class.equals(returnType)) {
        if (value == null) {
            return null;
        } else {
            return returnType.cast(LocaleUtils.toLocale(value));
        }
    } else if (Date.class.equals(returnType)) {
        if (value == null) {
            return null;
        } else {
            try {
                return returnType.cast(ISODateTimeFormat.dateTimeParser().parseDateTime(value).toDate());
            } catch (final IllegalArgumentException e) {
                e.printStackTrace();
                return null;
            }
        }
    } else if (DateTime.class.equals(returnType)) {
        if (value == null) {
            return null;
        } else {
            try {
                return returnType.cast(ISODateTimeFormat.dateTimeParser().parseDateTime(value));
            } catch (final IllegalArgumentException e) {
                e.printStackTrace();
                return null;
            }
        }
    } else if (String.class.equals(returnType)) {
        return returnType.cast(value);
    } else {
        throw new IllegalArgumentException(
                "primitive type " + returnType.getName() + " not supported by transformation");
    }
}

From source file:org.apache.marmotta.kiwi.loader.generic.KiWiHandler.java

License:Apache License

protected KiWiLiteral createLiteral(Literal l) throws ExecutionException {
    String value = l.getLabel();//ww w. j  a va2s  . c  o  m
    String lang = l.getLanguage() != null ? l.getLanguage().intern() : null;
    URI type = l.getDatatype();

    Locale locale;
    if (lang != null) {
        locale = localeCache.get(lang);
    } else {
        locale = null;
    }
    if (locale == null) {
        lang = null;
    }

    KiWiLiteral result;
    final KiWiUriResource rtype = type == null ? null : (KiWiUriResource) convertNode(type);

    try {

        try {
            // differentiate between the different types of the value
            if (type == null) {
                // FIXME: MARMOTTA-39 (this is to avoid a NullPointerException in the following if-clauses)
                result = connection.loadLiteral(sanitizeString(value), lang, null);

                if (result == null) {
                    result = new KiWiStringLiteral(sanitizeString(value), locale, null, importDate);
                } else {
                    nodesLoaded++;
                }
            } else if (type.equals(XSD.DateTime) || type.equals(XSD.Date) || type.equals(XSD.Time)) {
                // parse if necessary
                final DateTime dvalue = ISODateTimeFormat.dateTimeParser().withOffsetParsed()
                        .parseDateTime(value);

                result = connection.loadLiteral(dvalue);

                if (result == null) {
                    result = new KiWiDateLiteral(dvalue, rtype, importDate);
                } else {
                    nodesLoaded++;
                }
            } else if (type.equals(XSD.Integer) || type.equals(XSD.Long)) {
                long ivalue = Long.parseLong(value);

                result = connection.loadLiteral(ivalue);

                if (result == null) {
                    result = new KiWiIntLiteral(ivalue, rtype, importDate);
                } else {
                    nodesLoaded++;
                }
            } else if (type.equals(XSD.Double) || type.equals(XSD.Float) || type.equals(XSD.Decimal)) {
                double dvalue = Double.parseDouble(value);

                result = connection.loadLiteral(dvalue);

                if (result == null) {
                    result = new KiWiDoubleLiteral(dvalue, rtype, importDate);
                } else {
                    nodesLoaded++;
                }
            } else if (type.equals(XSD.Boolean)) {
                boolean bvalue = Boolean.parseBoolean(value);

                result = connection.loadLiteral(bvalue);

                if (result == null) {
                    result = new KiWiBooleanLiteral(bvalue, rtype, importDate);
                } else {
                    nodesLoaded++;
                }
            } else {
                result = connection.loadLiteral(sanitizeString(value), lang, rtype);

                if (result == null) {
                    result = new KiWiStringLiteral(sanitizeString(value), locale, rtype, importDate);
                } else {
                    nodesLoaded++;
                }
            }
        } catch (IllegalArgumentException ex) {
            // malformed number or date
            log.warn("malformed argument for typed literal of type {}: {}", rtype, value);
            KiWiUriResource mytype = createURI(Namespaces.NS_XSD + "string");

            result = connection.loadLiteral(sanitizeString(value), lang, mytype);

            if (result == null) {
                result = new KiWiStringLiteral(sanitizeString(value), locale, mytype, importDate);
            } else {
                nodesLoaded++;
            }

        }

        if (result.getId() < 0) {
            storeNode(result);
        }

        return result;

    } catch (SQLException e) {
        log.error("database error, could not load literal", e);
        throw new IllegalStateException("database error, could not load literal", e);
    }
}