Example usage for org.joda.time DateTime parse

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

Introduction

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

Prototype

public static DateTime parse(String str, DateTimeFormatter formatter) 

Source Link

Document

Parses a DateTime from the specified string using a formatter.

Usage

From source file:org.neotree.export.SummaryPrintDocumentAdapter.java

License:Open Source License

private String formatDateTime(Context context, String value) {
    return (TextUtils.isEmpty(value)) ? context.getString(R.string.label_not_set)
            : DateTime.parse(value, ISODateTimeFormat.dateTimeNoMillis())
                    .toString(NeoTree.NeoTreeFormat.DATETIME);
}

From source file:org.neotree.model.realm.SessionValue.java

License:Open Source License

@SuppressWarnings("unchecked")
public <T> T getValue() {
    switch (getDataTypeAsObject()) {
    case BOOLEAN:
        return (T) getBooleanValue();
    case DATETIME:
    case DATE:// www . j a  v  a  2  s.c om
    case TIME:
        return (T) ((getStringValue() != null)
                ? DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                : null);
    case ID:
        return (T) getStringValue();
    case NUMBER:
        return (T) getDoubleValue();
    case PERIOD:
        return (T) ((getStringValue() != null) ? Period.parse(getStringValue(), ISOPeriodFormat.standard())
                : null);
    case SET_ID:
        return (T) getStringValue();
    case STRING:
        return (T) getStringValue();
    case VOID:
    default:
        return null;
    }
}

From source file:org.neotree.model.realm.SessionValue.java

License:Open Source License

@JsonIgnore
@SuppressWarnings("unchecked")
public String getValueAsFormattedString(Context context) {
    switch (getDataTypeAsObject()) {
    case BOOLEAN:
        return (getBooleanValue() != null && getBooleanValue()) ? "Yes" : "No";
    case DATETIME:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                        .toString(NeoTree.NeoTreeFormat.DATETIME);
    case DATE://from  w w w . java2 s  .co  m
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                        .toString(NeoTree.NeoTreeFormat.DATE);
    case SET_ID:
    case ID:
        //return String.format("%s - %s", getStringValue(), getValueLabel());
        return getValueLabel();
    case NUMBER:
        final int decimalDigits = (TextUtils.isEmpty(getFormat())) ? 0 : getFormat().length();
        return (getDoubleValue() == null) ? context.getString(R.string.label_not_set)
                : String.format("%." + decimalDigits + "f", getDoubleValue());
    case PERIOD:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : Period.parse(getStringValue(), ISOPeriodFormat.standard())
                        .toString(NeoTree.NeoTreeFormat.PERIOD);
    case STRING:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set) : getStringValue();
    case TIME:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                        .toString(NeoTree.NeoTreeFormat.TIME);
    case VOID:
    default:
        break;
    }
    return "";
}

From source file:org.neotree.model.realm.SessionValue.java

License:Open Source License

@JsonIgnore
@SuppressWarnings("unchecked")
public String getValueAsExportString(Context context) {
    switch (getDataTypeAsObject()) {
    case BOOLEAN:
        return (getBooleanValue() != null && getBooleanValue()) ? "Yes" : "No";
    case DATETIME:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                        .toString(NeoTree.NeoTreeFormat.DATETIME_EXPORT);
    case DATE:/*from   w w  w  .j  a v  a 2  s.c o m*/
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                        .toString(NeoTree.NeoTreeFormat.DATE_EXPORT);
    case ID:
    case STRING:
        return getStringValue();
    case NUMBER:
        final int decimalDigits = (TextUtils.isEmpty(getFormat())) ? 0 : getFormat().length();
        return (getDoubleValue() == null) ? context.getString(R.string.label_not_set)
                : String.format("%." + decimalDigits + "f", getDoubleValue());
    case PERIOD:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : Period.parse(getStringValue(), ISOPeriodFormat.standard())
                        .toString(NeoTree.NeoTreeFormat.PERIOD);
    case TIME:
        return (getStringValue() == null) ? context.getString(R.string.label_not_set)
                : DateTime.parse(getStringValue(), ISODateTimeFormat.dateTimeNoMillis())
                        .toString(NeoTree.NeoTreeFormat.TIME);
    case SET_ID:
    case VOID:
    default:
        break;
    }
    return null;
}

From source file:org.nodel.reflection.Serialisation.java

License:Mozilla Public License

private static DateTime tryParseFullDateTime(String value) {
    try {/*from  w ww  .j a  va 2s.c om*/
        return DateTime.parse(value, DateTimeFormat.fullDateTime());
    } catch (Exception exc) {
        return null;
    }
}

From source file:org.nodel.reflection.Serialisation.java

License:Mozilla Public License

private static DateTime tryParseLongDateTime(String value) {
    try {/*from w ww  . j av  a  2s  .co m*/
        return DateTime.parse(value, DateTimeFormat.longDateTime());
    } catch (Exception exc) {
        return null;
    }
}

From source file:org.nodel.reflection.Serialisation.java

License:Mozilla Public License

private static DateTime tryParseMediumDateTime(String value) {
    try {//from  w ww . j  a  v a 2s. c om
        return DateTime.parse(value, DateTimeFormat.mediumDateTime());
    } catch (Exception exc) {
        return null;
    }
}

From source file:org.nodel.reflection.Serialisation.java

License:Mozilla Public License

private static DateTime tryParseShortDateTime(String value) {
    try {/* w ww .  j av  a  2  s.c  o m*/
        return DateTime.parse(value, DateTimeFormat.shortDateTime());
    } catch (Exception exc) {
        return null;
    }
}

From source file:org.nodel.reflection.Serialisation.java

License:Mozilla Public License

private static DateTime tryParseCustomFullDateTime(String value) {
    try {//ww w .ja v  a 2s  .  c  om
        return DateTime.parse(value, _customFullFormatter);
    } catch (Exception exc) {
        return null;
    }
}

From source file:org.openmrs.module.webservices.rest.web.ConversionUtil.java

License:Open Source License

/**
 * Converts the given object to the given type
 * //from w  w  w  .j a v  a 2s .co m
 * @param object
 * @param toType a simple class or generic type
 * @return
 * @throws ConversionException
 * @should convert strings to locales
 * @should convert strings to enum values
 * @should convert to an array
 * @should convert to a class
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Object convert(Object object, Type toType) throws ConversionException {
    if (object == null)
        return object;

    Class<?> toClass = toType instanceof Class ? ((Class<?>) toType)
            : (Class<?>) (((ParameterizedType) toType).getRawType());

    // if we're trying to convert _to_ a collection, handle it as a special case
    if (Collection.class.isAssignableFrom(toClass) || toClass.isArray()) {
        if (!(object instanceof Collection))
            throw new ConversionException("Can only convert a Collection to a Collection/Array. Not "
                    + object.getClass() + " to " + toType, null);

        if (toClass.isArray()) {
            Class<?> targetElementType = toClass.getComponentType();
            Collection input = (Collection) object;
            Object ret = Array.newInstance(targetElementType, input.size());

            int i = 0;
            for (Object element : (Collection) object) {
                Array.set(ret, i, convert(element, targetElementType));
                ++i;
            }
            return ret;
        }

        Collection ret = null;
        if (SortedSet.class.isAssignableFrom(toClass)) {
            ret = new TreeSet();
        } else if (Set.class.isAssignableFrom(toClass)) {
            ret = new HashSet();
        } else if (List.class.isAssignableFrom(toClass)) {
            ret = new ArrayList();
        } else {
            throw new ConversionException("Don't know how to handle collection class: " + toClass, null);
        }

        if (toType instanceof ParameterizedType) {
            // if we have generic type information for the target collection, we can use it to do conversion
            ParameterizedType toParameterizedType = (ParameterizedType) toType;
            Type targetElementType = toParameterizedType.getActualTypeArguments()[0];
            for (Object element : (Collection) object)
                ret.add(convert(element, targetElementType));
        } else {
            // otherwise we must just add all items in a non-type-safe manner
            ret.addAll((Collection) object);
        }
        return ret;
    }

    // otherwise we're converting _to_ a non-collection type

    if (toClass.isAssignableFrom(object.getClass()))
        return object;

    // Numbers with a decimal are always assumed to be Double, so convert to Float, if necessary
    if (toClass.isAssignableFrom(Float.class) && object instanceof Double) {
        return new Float((Double) object);
    }

    if (object instanceof String) {
        String string = (String) object;
        Converter<?> converter = getConverter(toClass);
        if (converter != null)
            return converter.getByUniqueId(string);

        if (toClass.isAssignableFrom(Date.class)) {
            IllegalArgumentException pex = null;
            String[] supportedFormats = { "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd'T'HH:mm:ss.SSS",
                    "yyyy-MM-dd'T'HH:mm:ssZ", "yyyy-MM-dd'T'HH:mm:ssXXX", "yyyy-MM-dd'T'HH:mm:ss",
                    "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" };
            for (int i = 0; i < supportedFormats.length; i++) {
                try {
                    Date date = DateTime.parse(string, DateTimeFormat.forPattern(supportedFormats[i])).toDate();
                    return date;
                } catch (IllegalArgumentException ex) {
                    pex = ex;
                }
            }
            throw new ConversionException(
                    "Error converting date - correct format (ISO8601 Long): yyyy-MM-dd'T'HH:mm:ss.SSSZ", pex);
        } else if (toClass.isAssignableFrom(Locale.class)) {
            return LocaleUtility.fromSpecification(object.toString());
        } else if (toClass.isEnum()) {
            return Enum.valueOf((Class<? extends Enum>) toClass, object.toString());
        } else if (toClass.isAssignableFrom(Class.class)) {
            try {
                return Context.loadClass((String) object);
            } catch (ClassNotFoundException e) {
                throw new ConversionException("Could not convert from " + object.getClass() + " to " + toType,
                        e);
            }
        }
        // look for a static valueOf(String) method (e.g. Double, Integer, Boolean)
        try {
            Method method = toClass.getMethod("valueOf", String.class);
            if (Modifier.isStatic(method.getModifiers()) && toClass.isAssignableFrom(method.getReturnType())) {
                return method.invoke(null, string);
            }
        } catch (Exception ex) {
        }
    } else if (object instanceof Map) {
        return convertMap((Map<String, ?>) object, toClass);
    }
    if (toClass.isAssignableFrom(Double.class) && object instanceof Number) {
        return ((Number) object).doubleValue();
    } else if (toClass.isAssignableFrom(Integer.class) && object instanceof Number) {
        return ((Number) object).intValue();
    }

    if (toClass.isAssignableFrom(String.class) && object instanceof Boolean) {
        return String.valueOf(object);
    }

    throw new ConversionException("Don't know how to convert from " + object.getClass() + " to " + toType,
            null);
}