{"record":{"id":"10df20de01f1f172","repo":"pentaho/pentaho-kettle","slug":"could-not-apply-the-given-format-sarg2-on-the-string-for","errorCode":null,"errorMessage":"Could not apply the given format \" + sArg2 + \" on the string for \" + sArg1 + \" : \" + e.getMessage()","messagePattern":"Could not apply the given format \" \\+ sArg2 \\+ \" on the string for \" \\+ sArg1 \\+ \" : \" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1024,"sourceCode":"          // DecimalFormat formatter = new DecimalFormat();\n          // sRC= formatter.format(sArg1);\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not apply local format for \" + sArg1 + \" : \" + e.getMessage() );\n        }\n        break;\n      case 2:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {\n            return undefinedValue;\n          }\n          sArg1 = (String) ArgList[0];\n          sArg2 = (String) ArgList[1];\n          Format dfFormatter = new SimpleDateFormat( sArg2 );\n          oRC = dfFormatter.parseObject( sArg1 );\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not apply the given format \"\n            + sArg2 + \" on the string for \" + sArg1 + \" : \" + e.getMessage() );\n        }\n        break;\n      case 3:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {\n            return undefinedValue;\n          }\n          sArg1 = (String) ArgList[0];\n          Format dfFormatter;\n          sArg2 = (String) ArgList[1];\n          sArg3 = (String) ArgList[2];\n          if ( sArg3.length() == 2 ) {\n            Locale dfLocale = EnvUtil.createLocale( sArg3 );\n            dfFormatter = new SimpleDateFormat( sArg2, dfLocale );\n            oRC = dfFormatter.parseObject( sArg1 );","sourceCodeStart":1006,"sourceCodeEnd":1042,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1006-L1042","documentation":"When str2date() is called with two arguments, the second argument is used as a SimpleDateFormat pattern. If the string cannot be parsed with that pattern, this RuntimeException is thrown, naming the format and the string plus the parser message. Common wrapped causes are ParseException and IllegalArgumentException for an invalid pattern itself.","triggerScenarios":"Calling str2date('2026-09-13','dd/MM/yyyy') where the string doesn't match the pattern, or str2date(x,'bogus-pattern') where the pattern itself is invalid (IllegalArgumentException).","commonSituations":"Mixing up MM (month) and mm (minute); using yyyy vs yy incorrectly; passing a timestamp string with time components to a date-only pattern; null or empty string argument.","solutions":["Make the pattern exactly match the input string, including separators and component widths (e.g. 'yyyy-MM-dd HH:mm:ss').","Check the wrapped message in the RuntimeException: ParseException indicates a data/pattern mismatch, IllegalArgumentException indicates a bad pattern.","Use MM for months and mm for minutes; verify literal separators match the data.","Trim inputs and handle null/empty in script before calling str2date."],"exampleFix":"// before\nvar d = str2date('2026-09-13', 'dd/MM/yyyy');\n// after\nvar d = str2date('2026-09-13', 'yyyy-MM-dd');","handlingStrategy":"try-catch","validationCode":"if (s != null && String(s).trim().length > 0 && /^[yMdHms\\-\\/: .]+$/.test(fmt)) { var d = str2date(s, fmt); }","typeGuard":null,"tryCatchPattern":"try { var d = str2date(s, fmt); } catch (e) { logError('format ' + fmt + ' failed on ' + s + ': ' + e.message); d = null; }","preventionTips":["Match the pattern exactly to the data (use MM for months, mm for minutes)","Include time components in the pattern when data contains them","Sample real data values to derive the correct pattern","Distinguish ParseException (data mismatch) from IllegalArgumentException (bad pattern) in messages"],"tags":["date","format","parsing","scripting"],"backgroundTag":"invalid-date-format","analyzedSha":"f3058517a153da500bf4551f46d79b91bf8ec552","analyzedAt":"2026-09-13T14:04:16.340Z","contentChangedAt":"2026-09-13T14:04:16.340Z","schemaVersion":2},"datasetVersion":"2026-09-20T23:17:15.980Z"}