Example usage for java.text Format parseObject

List of usage examples for java.text Format parseObject

Introduction

In this page you can find the example usage for java.text Format parseObject.

Prototype

public Object parseObject(String source) throws ParseException 

Source Link

Document

Parses text from the beginning of the given string to produce an object.

Usage

From source file:com.anrisoftware.propertiesutils.ContextProperties.java

/**
 * Returns a property of the specified type.
 * //ww w .j  a v a 2 s.  com
 * @param key
 *            the property key.
 * 
 * @param defaultValue
 *            the default value.
 * 
 * @param format
 *            the {@link Format} to parse the type.
 * 
 * @return the the property or {@code null} if no property with the key was
 *         found.
 * 
 * @throws ParseException
 *             if the property cannot be parsed to the type.
 */
@SuppressWarnings("unchecked")
public <T> T getTypedProperty(String key, Format format, T defaultValue) throws ParseException {
    String property = getProperty(key, String.valueOf(defaultValue));
    return property == null ? null : (T) format.parseObject(property);
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object str2date(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    Object oRC = new Object();
    String sArg1 = "";
    String sArg2 = "";
    String sArg3 = "";
    String sArg4 = "";
    switch (ArgList.length) {
    case 0:/*  w ww  . java 2s .c om*/
        throw new RuntimeException("Please provide a valid string to the function call str2date.");
    case 1:
        try {
            if (isNull(ArgList[0]))
                return null;
            else if (isUndefined(ArgList[0]))
                return undefinedValue;
            sArg1 = (String) ArgList[0];
            Format dfFormatter = new SimpleDateFormat();
            oRC = dfFormatter.parseObject(sArg1);
            //if(Double.isNaN(sArg1))   throw new RuntimeException("The first Argument must be a Number.");
            //DecimalFormat formatter = new DecimalFormat();
            //sRC= formatter.format(sArg1); 
        } catch (Exception e) {
            throw new RuntimeException("Could not apply local format for " + sArg1 + " : " + e.getMessage());
        }
        break;
    case 2:
        try {
            if (isNull(ArgList, new int[] { 0, 1 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1 }))
                return undefinedValue;
            sArg1 = (String) ArgList[0];
            sArg2 = (String) ArgList[1];
            Format dfFormatter = new SimpleDateFormat(sArg2);
            oRC = dfFormatter.parseObject(sArg1);
        } catch (Exception e) {
            throw new RuntimeException("Could not apply the given format " + sArg2 + " on the string for "
                    + sArg1 + " : " + e.getMessage());
        }
        break;
    case 3:
        try {
            if (isNull(ArgList, new int[] { 0, 1, 2 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1, 2 }))
                return undefinedValue;
            sArg1 = (String) ArgList[0];
            Format dfFormatter;
            sArg2 = (String) ArgList[1];
            sArg3 = (String) ArgList[2];
            if (sArg3.length() == 2) {
                Locale dfLocale = new Locale(sArg3);
                dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                oRC = dfFormatter.parseObject(sArg1);
            } else {
                throw new RuntimeException("Locale " + sArg3 + " is not 2 characters long.");
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2
                            + " on the string for " + sArg1 + " : " + e.getMessage());
        }
        break;
    case 4:
        try {
            if (isNull(ArgList, new int[] { 0, 1, 2, 3 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1, 2, 3 }))
                return undefinedValue;
            sArg1 = (String) ArgList[0];
            DateFormat dfFormatter;
            sArg2 = (String) ArgList[1];
            sArg3 = (String) ArgList[2];
            sArg4 = (String) ArgList[3];

            // If the timezone is not recognized, java will automatically
            // take GMT.
            TimeZone tz = TimeZone.getTimeZone(sArg4);

            if (sArg3.length() == 2) {
                Locale dfLocale = new Locale(sArg3);
                dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                dfFormatter.setTimeZone(tz);
                oRC = dfFormatter.parseObject(sArg1);
            } else {
                throw new RuntimeException("Locale " + sArg3 + " is not 2 characters long.");
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2
                            + " on the string for " + sArg1 + " : " + e.getMessage());
        }
        break;
    default:
        throw new RuntimeException("The function call str2date requires 1, 2, 3, or 4 arguments.");
    }
    return oRC;
}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static Object str2date(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {
    Object oRC = new Object();
    String sArg1 = "";
    String sArg2 = "";
    String sArg3 = "";
    String sArg4 = "";
    switch (ArgList.length) {
    case 0:// w  w  w  .  j a  va 2 s . co m
        throw Context.reportRuntimeError("Please provide a valid string to the function call str2date.");
    case 1:
        try {
            if (isNull(ArgList[0]))
                return null;
            else if (isUndefined(ArgList[0]))
                return Context.getUndefinedValue();
            sArg1 = Context.toString(ArgList[0]);
            Format dfFormatter = new SimpleDateFormat();
            oRC = dfFormatter.parseObject(sArg1);
            //if(Double.isNaN(sArg1))   throw Context.reportRuntimeError("The first Argument must be a Number.");
            //DecimalFormat formatter = new DecimalFormat();
            //sRC= formatter.format(sArg1); 
        } catch (Exception e) {
            throw Context
                    .reportRuntimeError("Could not apply local format for " + sArg1 + " : " + e.getMessage());
        }
        break;
    case 2:
        try {
            if (isNull(ArgList, new int[] { 0, 1 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1 }))
                return Context.getUndefinedValue();
            sArg1 = Context.toString(ArgList[0]);
            sArg2 = Context.toString(ArgList[1]);
            Format dfFormatter = new SimpleDateFormat(sArg2);
            oRC = dfFormatter.parseObject(sArg1);
        } catch (Exception e) {
            throw Context.reportRuntimeError("Could not apply the given format " + sArg2 + " on the string for "
                    + sArg1 + " : " + e.getMessage());
        }
        break;
    case 3:
        try {
            if (isNull(ArgList, new int[] { 0, 1, 2 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1, 2 }))
                return Context.getUndefinedValue();
            sArg1 = Context.toString(ArgList[0]);
            Format dfFormatter;
            sArg2 = Context.toString(ArgList[1]);
            sArg3 = Context.toString(ArgList[2]);
            if (sArg3.length() == 2) {
                Locale dfLocale = new Locale(sArg3);
                dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                oRC = dfFormatter.parseObject(sArg1);
            } else {
                throw Context.reportRuntimeError("Locale " + sArg3 + " is not 2 characters long.");
            }
        } catch (Exception e) {
            throw Context.reportRuntimeError(
                    "Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2
                            + " on the string for " + sArg1 + " : " + e.getMessage());
        }
        break;
    case 4:
        try {
            if (isNull(ArgList, new int[] { 0, 1, 2, 3 }))
                return null;
            else if (isUndefined(ArgList, new int[] { 0, 1, 2, 3 }))
                return Context.getUndefinedValue();
            sArg1 = Context.toString(ArgList[0]);
            DateFormat dfFormatter;
            sArg2 = Context.toString(ArgList[1]);
            sArg3 = Context.toString(ArgList[2]);
            sArg4 = Context.toString(ArgList[3]);

            // If the timezone is not recognized, java will automatically
            // take GMT.
            TimeZone tz = TimeZone.getTimeZone(sArg4);

            if (sArg3.length() == 2) {
                Locale dfLocale = new Locale(sArg3);
                dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                dfFormatter.setTimeZone(tz);
                oRC = dfFormatter.parseObject(sArg1);
            } else {
                throw Context.reportRuntimeError("Locale " + sArg3 + " is not 2 characters long.");
            }
        } catch (Exception e) {
            throw Context.reportRuntimeError(
                    "Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2
                            + " on the string for " + sArg1 + " : " + e.getMessage());
        }
        break;
    default:
        throw Context.reportRuntimeError("The function call str2date requires 1, 2, 3, or 4 arguments.");
    }
    return oRC;
}

From source file:com.akretion.kettle.steps.terminatooor.ScriptValuesAddedFunctions.java

public static Object getFiscalDate(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {

    if (ArgList.length == 2) {
        try {//from w  ww  .  j  a va  2 s.co  m
            if (isNull(ArgList))
                return null;
            else if (isUndefined(ArgList))
                return undefinedValue;
            java.util.Date dIn = (java.util.Date) ArgList[0];
            Calendar startDate = Calendar.getInstance();
            Calendar fisStartDate = Calendar.getInstance();
            Calendar fisOffsetDate = Calendar.getInstance();
            startDate.setTime(dIn);
            Format dfFormatter = new SimpleDateFormat("dd.MM.yyyy");
            String strOffsetDate = (String) ArgList[1] + String.valueOf(startDate.get(Calendar.YEAR));
            java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject(strOffsetDate);
            fisOffsetDate.setTime(dOffset);

            String strFisStartDate = "01.01." + String.valueOf(startDate.get(Calendar.YEAR) + 1);
            fisStartDate.setTime((java.util.Date) dfFormatter.parseObject(strFisStartDate));
            int iDaysToAdd = (int) ((startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis()) / 86400000);
            fisStartDate.add(Calendar.DATE, iDaysToAdd);
            return fisStartDate.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call getFiscalDate requires 2 arguments.");
    }

}

From source file:com.panet.imeta.trans.steps.scriptvalues_mod.ScriptValuesAddedFunctions.java

public static Object getFiscalDate(Context actualContext, Scriptable actualObject, Object[] ArgList,
        Function FunctionContext) {

    if (ArgList.length == 2) {
        try {//w  w w . j ava 2  s .  co m
            if (isNull(ArgList))
                return null;
            else if (isUndefined(ArgList))
                return Context.getUndefinedValue();
            java.util.Date dIn = (java.util.Date) Context.jsToJava(ArgList[0], java.util.Date.class);
            Calendar startDate = Calendar.getInstance();
            Calendar fisStartDate = Calendar.getInstance();
            Calendar fisOffsetDate = Calendar.getInstance();
            startDate.setTime(dIn);
            Format dfFormatter = new SimpleDateFormat("dd.MM.yyyy");
            String strOffsetDate = Context.toString(ArgList[1]) + String.valueOf(startDate.get(Calendar.YEAR));
            java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject(strOffsetDate);
            fisOffsetDate.setTime(dOffset);

            String strFisStartDate = "01.01." + String.valueOf(startDate.get(Calendar.YEAR) + 1);
            fisStartDate.setTime((java.util.Date) dfFormatter.parseObject(strFisStartDate));
            int iDaysToAdd = (int) ((startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis()) / 86400000);
            fisStartDate.add(Calendar.DATE, iDaysToAdd);
            return fisStartDate.getTime();
        } catch (Exception e) {
            throw Context.reportRuntimeError(e.toString());
        }
    } else {
        throw Context.reportRuntimeError("The function call getFiscalDate requires 2 arguments.");
    }

}

From source file:com.projity.field.Field.java

public final void setText(Node node, WalkersNodeModel nodeModel, String textValue, FieldContext context)
        throws FieldParseException {
    if (context == null)
        context = specialFieldContext;/*  w  ww  .j  av  a2 s.c  o m*/
    if (isReadOnly(node, nodeModel, context)) { // don't allow setting of
        // read only fields
        // log.warn("Tried to set text of read only field" + getId());
        return;
    }
    Object object = node.getImpl();
    Object value = preprocessText(object, textValue, context);
    if (value == textValue) {
        Format f = getFormat(object);
        if (f != null) {
            try {
                value = f.parseObject(textValue);
            } catch (ParseException e) {
                throw new FieldParseException(e);
            }
        } else {
            value = FieldConverter.convert(textValue, hasExternalType() ? externalType : internalType, context); // converts
            // to
            // Date
        }
    }
    setValue(node, nodeModel, this, value, context);
}

From source file:com.projity.field.Field.java

public final void setText(Object object, String textValue, FieldContext context) throws FieldParseException {
    if (context == null)
        context = specialFieldContext;//www . j  ava 2 s  .c o m
    if (isReadOnly(object, context)) { // don't allow setting of read only
        // fields
        // log.warn("Tried to set text of read only field" + getId());
        return;
    }
    Object value = preprocessText(object, textValue, context);
    if (value == textValue) {
        Format f = getFormat(object);
        if (f != null) {
            try {
                value = f.parseObject(textValue);
            } catch (ParseException e) {
                f = getSecondaryFormat(object); // allow money to parse as number too
                boolean secondOK = false;
                if (f != null) {
                    try {
                        value = f.parseObject(textValue);
                        secondOK = true;
                    } catch (ParseException e1) {
                    }
                }
                if (!secondOK)
                    throw new FieldParseException(e);
            }
        } else {
            value = FieldConverter.convert(textValue, hasExternalType() ? externalType : internalType, context); // converts
            // to
            // Date
        }
    }
    setInternalValueAndUpdate(object, this, value, context);
}

From source file:org.getobjects.appserver.elements.WOFormatter.java

/**
 * This method extracts the java.text.Format object of the formatter. It then
 * calls parseObject() on the given _s string.
 * <p>/*  w  w  w .  j  a  v  a2s  .  c o  m*/
 * As a special hack, it converts Long objects into Integer objects when the
 * latter can cover the scope.
 *
 * @param _s   - String to parse
 * @param _ctx - WOContext in which the operation takes place
 * @return the parsed Object
 */
public Object objectValueForString(final String _s, final WOContext _ctx) throws ParseException {
    if (_s == null)
        return null;

    final Format fmt = this.formatInContext(_ctx);
    //System.err.println("FORMAT WITH: " + fmt);
    //System.err.println("  value: " + _s + " [" + _s.getClass() + "]");
    if (fmt == null)
        return _s;

    Object v = fmt.parseObject(_s);

    /* Downcast large values, eg if a Long fits into an Integer,
     * we return an Integer.
     * A bit hackish, but better for various reasons (eg JS bridge)
     */
    if (v instanceof Long) {
        long l = ((Long) v).longValue();
        if (l <= Integer.MAX_VALUE && l >= Integer.MIN_VALUE)
            v = new Integer((int) l);
    }
    //System.err.println("  IS: " + v + " [" + v.getClass() + "]");
    return v;
}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static Object getFiscalDate(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {

    if (ArgList.length == 2) {
        try {//from w  w w . ja  va 2  s .co m
            if (isNull(ArgList)) {
                return null;
            } else if (isUndefined(ArgList)) {
                return undefinedValue;
            }
            java.util.Date dIn = (java.util.Date) ArgList[0];
            Calendar startDate = Calendar.getInstance();
            Calendar fisStartDate = Calendar.getInstance();
            Calendar fisOffsetDate = Calendar.getInstance();
            startDate.setTime(dIn);
            Format dfFormatter = new SimpleDateFormat("dd.MM.yyyy");
            String strOffsetDate = (String) ArgList[1] + String.valueOf(startDate.get(Calendar.YEAR));
            java.util.Date dOffset = (java.util.Date) dfFormatter.parseObject(strOffsetDate);
            fisOffsetDate.setTime(dOffset);

            String strFisStartDate = "01.01." + String.valueOf(startDate.get(Calendar.YEAR) + 1);
            fisStartDate.setTime((java.util.Date) dfFormatter.parseObject(strFisStartDate));
            int iDaysToAdd = (int) ((startDate.getTimeInMillis() - fisOffsetDate.getTimeInMillis()) / 86400000);
            fisStartDate.add(Calendar.DATE, iDaysToAdd);
            return fisStartDate.getTime();
        } catch (Exception e) {
            throw new RuntimeException(e.toString());
        }
    } else {
        throw new RuntimeException("The function call getFiscalDate requires 2 arguments.");
    }

}

From source file:org.pentaho.di.trans.steps.script.ScriptAddedFunctions.java

public static Object str2date(ScriptEngine actualContext, Bindings actualObject, Object[] ArgList,
        Object FunctionContext) {
    Object oRC = new Object();
    String sArg1 = "";
    String sArg2 = "";
    String sArg3 = "";
    String sArg4 = "";
    switch (ArgList.length) {
    case 0://from w  w w.  j  a v  a  2s. c o m
        throw new RuntimeException("Please provide a valid string to the function call str2date.");
    case 1:
        try {
            if (isNull(ArgList[0])) {
                return null;
            } else if (isUndefined(ArgList[0])) {
                return undefinedValue;
            }
            sArg1 = (String) ArgList[0];
            Format dfFormatter = new SimpleDateFormat();
            oRC = dfFormatter.parseObject(sArg1);
            // if(Double.isNaN(sArg1)) throw new RuntimeException("The first Argument must be a Number.");
            // DecimalFormat formatter = new DecimalFormat();
            // sRC= formatter.format(sArg1);
        } catch (Exception e) {
            throw new RuntimeException("Could not apply local format for " + sArg1 + " : " + e.getMessage());
        }
        break;
    case 2:
        try {
            if (isNull(ArgList, new int[] { 0, 1 })) {
                return null;
            } else if (isUndefined(ArgList, new int[] { 0, 1 })) {
                return undefinedValue;
            }
            sArg1 = (String) ArgList[0];
            sArg2 = (String) ArgList[1];
            Format dfFormatter = new SimpleDateFormat(sArg2);
            oRC = dfFormatter.parseObject(sArg1);
        } catch (Exception e) {
            throw new RuntimeException("Could not apply the given format " + sArg2 + " on the string for "
                    + sArg1 + " : " + e.getMessage());
        }
        break;
    case 3:
        try {
            if (isNull(ArgList, new int[] { 0, 1, 2 })) {
                return null;
            } else if (isUndefined(ArgList, new int[] { 0, 1, 2 })) {
                return undefinedValue;
            }
            sArg1 = (String) ArgList[0];
            Format dfFormatter;
            sArg2 = (String) ArgList[1];
            sArg3 = (String) ArgList[2];
            if (sArg3.length() == 2) {
                Locale dfLocale = EnvUtil.createLocale(sArg3);
                dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                oRC = dfFormatter.parseObject(sArg1);
            } else {
                throw new RuntimeException("Locale " + sArg3 + " is not 2 characters long.");
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2
                            + " on the string for " + sArg1 + " : " + e.getMessage());
        }
        break;
    case 4:
        try {
            if (isNull(ArgList, new int[] { 0, 1, 2, 3 })) {
                return null;
            } else if (isUndefined(ArgList, new int[] { 0, 1, 2, 3 })) {
                return undefinedValue;
            }
            sArg1 = (String) ArgList[0];
            DateFormat dfFormatter;
            sArg2 = (String) ArgList[1];
            sArg3 = (String) ArgList[2];
            sArg4 = (String) ArgList[3];

            // If the timezone is not recognized, java will automatically
            // take GMT.
            TimeZone tz = TimeZone.getTimeZone(sArg4);

            if (sArg3.length() == 2) {
                Locale dfLocale = EnvUtil.createLocale(sArg3);
                dfFormatter = new SimpleDateFormat(sArg2, dfLocale);
                dfFormatter.setTimeZone(tz);
                oRC = dfFormatter.parseObject(sArg1);
            } else {
                throw new RuntimeException("Locale " + sArg3 + " is not 2 characters long.");
            }
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not apply the local format for locale " + sArg3 + " with the given format " + sArg2
                            + " on the string for " + sArg1 + " : " + e.getMessage());
        }
        break;
    default:
        throw new RuntimeException("The function call str2date requires 1, 2, 3, or 4 arguments.");
    }
    return oRC;
}