Example usage for org.apache.commons.beanutils.converters DateTimeConverter setPattern

List of usage examples for org.apache.commons.beanutils.converters DateTimeConverter setPattern

Introduction

In this page you can find the example usage for org.apache.commons.beanutils.converters DateTimeConverter setPattern.

Prototype

public void setPattern(String pattern) 

Source Link

Document

Set a date format pattern to use to convert dates to/from a java.lang.String.

Usage

From source file:com.xpfriend.fixture.cast.temp.TypeConverter.java

private static Object convert(DateTimeConverter converter, String format, String value, Class<?> type) {
    try {//from  w ww  .  j  a v a 2  s  . co m
        return converter.convert(type, value);
    } catch (ConversionException e) {
        converter.setPattern(format);
        return converter.convert(type, value);
    }
}

From source file:com.datatorrent.contrib.parser.RegexParser.java

@Override
public void processTuple(byte[] tuple) {
    if (tuple == null) {
        if (err.isConnected()) {
            err.emit(new KeyValPair<String, String>(null, "Blank/null tuple"));
        }//from ww w . ja  v  a 2s .  c o  m
        errorTupleCount++;
        return;
    }
    String incomingString = new String(tuple);
    if (StringUtils.isBlank(incomingString)) {
        if (err.isConnected()) {
            err.emit(new KeyValPair<String, String>(incomingString, "Blank tuple"));
        }
        errorTupleCount++;
        return;
    }
    try {
        if (out.isConnected() && clazz != null) {
            Matcher matcher = pattern.matcher(incomingString);
            boolean patternMatched = false;
            Constructor<?> ctor = clazz.getConstructor();
            Object object = ctor.newInstance();

            if (matcher.find()) {
                for (int i = 0; i <= matcher.groupCount() - 1; i++) {
                    if (delimitedParserSchema.getFields().get(i).getType() == DelimitedSchema.FieldType.DATE) {
                        DateTimeConverter dtConverter = new DateConverter();
                        dtConverter.setPattern((String) delimitedParserSchema.getFields().get(i)
                                .getConstraints().get(DelimitedSchema.DATE_FORMAT));
                        ConvertUtils.register(dtConverter, Date.class);
                    }
                    BeanUtils.setProperty(object, delimitedParserSchema.getFields().get(i).getName(),
                            matcher.group(i + 1));
                }
                patternMatched = true;
            }
            if (!patternMatched) {
                throw new ConversionException(
                        "The incoming tuple do not match with the Regex pattern defined.");
            }

            out.emit(object);
            emittedObjectCount++;
        }

    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | InstantiationException
            | ConversionException e) {
        if (err.isConnected()) {
            err.emit(new KeyValPair<String, String>(incomingString, e.getMessage()));
            logger.debug("Regex Expression : {} Incoming tuple : {}", splitRegexPattern, incomingString);
        }
        errorTupleCount++;
        logger.error("Tuple could not be parsed. Reason {}", e.getMessage());
    }
}