List of usage examples for java.text Format parseObject
public Object parseObject(String source) throws ParseException
From source file:org.pentaho.di.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. ja va 2 s. com*/ 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:org.pentaho.di.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://from w w w .ja v a 2 s .c o 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 = EnvUtil.createLocale(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 = EnvUtil.createLocale(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; }