{"record":{"id":"ffa371d144acdb5b","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-to-the-given-local-format-ffa371","errorCode":null,"errorMessage":"Could not convert to the given local format.","messagePattern":"Could not convert to the given local format\\.","errorType":"exception","errorClass":"JavaScriptException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java","lineNumber":1178,"sourceCode":"        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 Context.getUndefinedValue();\n          }\n          java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );\n          DateFormat dfFormatter;\n          String sArg2 = Context.toString( ArgList[1] );\n          String sArg3 = Context.toString( ArgList[2] );\n          if ( sArg3.length() == 2 ) {\n            Locale dfLocale = EnvUtil.createLocale( sArg3.toLowerCase() );\n            dfFormatter = new SimpleDateFormat( sArg2, dfLocale );\n            oRC = dfFormatter.format( dArg1 );\n          } else {\n            throw Context.reportRuntimeError( \"Locale is not 2 characters long.\" );\n          }\n        } catch ( Exception e ) {\n          throw Context.reportRuntimeError( \"Could not convert to the given local format.\" );\n        }\n        break;\n      case 4:\n        try {\n          if ( isNull( ArgList, new int[] { 0, 1, 2, 3 } ) ) {\n            return null;\n          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2, 3 } ) ) {\n            return Context.getUndefinedValue();\n          }\n          java.util.Date dArg1 = (java.util.Date) Context.jsToJava( ArgList[0], java.util.Date.class );\n          DateFormat dfFormatter;\n          String sArg2 = Context.toString( ArgList[1] );\n          String sArg3 = Context.toString( ArgList[2] );\n          String sArg4 = Context.toString( ArgList[3] );\n\n          // If the timezone is not recognized, java will automatically\n          // take GMT.\n          TimeZone tz = TimeZone.getTimeZone( sArg4 );","sourceCodeStart":1160,"sourceCodeEnd":1196,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/scriptvalues_mod/ScriptValuesAddedFunctions.java#L1160-L1196","documentation":"This error is thrown by the date2str JavaScript function in ScriptValuesAddedFunctions when formatting a Date with an explicit locale fails. The 3-argument form builds a SimpleDateFormat with a user-supplied pattern and a 2-letter locale string; any exception in that path (bad pattern, bad locale, wrong argument types) is caught and re-thrown as this generic runtime error. It deliberately masks the underlying exception, so the real cause must be inferred from the arguments.","triggerScenarios":"Calling date2str(date, pattern, locale) in a Kettle/JavaScript step where: the pattern contains illegal SimpleDateFormat characters (e.g. \"yyyy-mm-dd\" intended but with unquoted literals like 'T' handled wrong, or stray quotes), the locale argument is not exactly 2 characters (which first raises the 2-char check only when the check branch is reached; other arg-shape failures land here), the first argument is not a Date/number, or the arguments are null/undefined so SimpleDateFormat construction throws.","commonSituations":"Transformation developers passing a string timestamp instead of a Date object; using locale codes like 'en_US' (5 chars) instead of 'en'; typos in the format mask such as unmatched single quotes; migrating scripts between Kettle versions where date2str argument order or null handling changed.","solutions":["Validate the pattern with Java's SimpleDateFormat outside the script (new SimpleDateFormat(pattern, locale)) to see the real underlying exception.","Ensure the first argument is a Date (use new Date(...) or d2i/date parsing first) and not null/undefined.","Use a 2-letter ISO-639 locale code (e.g. 'de', 'fr'); longer codes are rejected before this path.","Quote literal characters in the pattern with single quotes (e.g. \"yyyy-MM-dd'T'HH:mm:ss\")."],"exampleFix":"// before\ndate2str(orderDate, \"yyyy-mm-dd hh:mm:ss\", \"en_US\")\n// after\ndate2str(orderDate, \"yyyy-MM-dd HH:mm:ss\", \"en\")","handlingStrategy":"validation","validationCode":"// JS in Kettle step\nfunction safeDate2str(d, fmt, loc) {\n  if (d == null || fmt == null || loc == null || loc.length != 2) return null;\n  return date2str(d, fmt, loc);\n}","typeGuard":"function isDate(v) { return v != null && typeof v.getTime === 'function'; }","tryCatchPattern":"try { result = date2str(d, fmt, loc); } catch (e) { result = null; /* log e for step failure diagnostics */ }","preventionTips":["Always pass a real Date object, not a string","Use 2-letter ISO-639 locale codes","Quote literal characters in SimpleDateFormat patterns","Test the pattern in plain Java when debugging"],"tags":["javascript","date-formatting","scripting","pentaho-kettle"],"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"}