{"record":{"id":"b94eaaca0f6d4475","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-to-the-given-local-format","errorCode":null,"errorMessage":"Could not convert to the given local format.","messagePattern":"Could not convert to the given local format\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1144,"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 undefinedValue;\n          }\n          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          DateFormat dfFormatter;\n          String sArg2 = (String) ArgList[1];\n          String sArg3 = (String) 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 new RuntimeException( \"Locale is not 2 characters long.\" );\n          }\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"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 undefinedValue;\n          }\n          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          DateFormat dfFormatter;\n          String sArg2 = (String) ArgList[1];\n          String sArg3 = (String) ArgList[2];\n          String sArg4 = (String) ArgList[3];\n\n          // If the timezone is not recognized, java will automatically\n          // take GMT.\n          TimeZone tz = TimeZone.getTimeZone( sArg4 );","sourceCodeStart":1126,"sourceCodeEnd":1162,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1126-L1162","documentation":"Catch-all rethrow for the 3-argument date2str branch (date, format, locale): any failure — non-Date first argument, invalid pattern, wrong-length locale, or locale the JDK doesn't recognize — is wrapped into 'Could not convert to the given local format.' with no underlying message attached.","triggerScenarios":"date2str(date, format, locale) throwing inside the try block: ClassCastException on the date, IllegalArgumentException from a bad SimpleDateFormat pattern, or the nested 'Locale is not 2 characters long' RuntimeException.","commonSituations":"String instead of Date; invalid format letters; locale of the wrong length or an unsupported code; nulls slipping past the isNull check because they arrive as non-null non-Date values.","solutions":["Confirm the first argument is a java.util.Date (convert strings with str2date first).","Verify the format is a valid SimpleDateFormat pattern and the locale is a 2-letter code.","Validate arguments before the call to avoid the opaque wrapper message.","Isolate the call in try/catch and log each argument's type/value to find the offender.","Compare with the working 2-argument form to determine whether format or locale is at fault."],"exampleFix":"// before\nvar s = date2str('2024-01-01', 'qqqq', 'en'); // String + bad pattern\n// after\nvar s = date2str(str2date('2024-01-01','yyyy-MM-dd'), 'yyyy-MM-dd', 'en');","handlingStrategy":"try-catch","validationCode":"function safeDate2str3(d, fmt, loc) {\n  if (!isJavaDate(d)) d = str2date(String(d), 'yyyy-MM-dd');\n  if (typeof loc !== 'string' || loc.length !== 2) loc = String(loc).substring(0, 2);\n  return date2str(d, fmt, loc);\n}","typeGuard":"function isJavaDate(v) { return v != null && typeof v === 'object' && v.getClass && String(v.getClass()).indexOf('java.util.Date') >= 0; }","tryCatchPattern":"try { var s = date2str(d, fmt, loc); } catch (e) { if (String(e.message).indexOf('Could not convert to the given local format') >= 0) { Logger.log('args: d=' + d + ' fmt=' + fmt + ' loc=' + loc); s = null; } else { throw e; } }","preventionTips":["Ensure arg1 is a java.util.Date (convert with str2date)","Keep the locale a 2-letter code and the format a valid SimpleDateFormat pattern","Log argument types/values on failure — the wrapper message omits the cause","Fall back to the 2-argument date2str form to isolate whether locale is the problem"],"tags":["javascript","date-formatting","locale","scripting"],"backgroundTag":"invalid-argument-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"}