{"record":{"id":"ba73d702bb411346","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-to-the-given-format","errorCode":null,"errorMessage":"Could not convert to the given format.","messagePattern":"Could not convert to the given format\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1122,"sourceCode":"          Format dfFormatter = new SimpleDateFormat();\n          oRC = dfFormatter.format( dArg1 );\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not convert to local format.\" );\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          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          String sArg2 = (String) ArgList[1];\n          Format dfFormatter = new SimpleDateFormat( sArg2 );\n          oRC = dfFormatter.format( dArg1 );\n        } catch ( Exception e ) {\n          throw new RuntimeException( \"Could not convert to the given format.\" );\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          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 {","sourceCodeStart":1104,"sourceCodeEnd":1140,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1104-L1140","documentation":"Catch-all rethrow for the 2-argument date2str form (date, format): any exception — the date not being a java.util.Date, or the second argument not being a valid format string — is wrapped into 'Could not convert to the given format.'. The underlying cause is not appended, so you must inspect the arguments.","triggerScenarios":"date2str(date, pattern) where ArgList[0] is not a java.util.Date (ClassCastException) or SimpleDateFormat rejects the pattern (IllegalArgumentException, e.g. malformed pattern).","commonSituations":"Passing a string/number instead of a Date; a garbled or empty format string; quotes or invalid pattern letters in the format.","solutions":["Verify the first argument is a java.util.Date; convert with str2date() if it is a string.","Check the format string is a valid SimpleDateFormat pattern (e.g. 'yyyy-MM-dd HH:mm:ss').","Guard null/non-date values before calling.","Test the pattern in plain Java or a JS snippet to confirm validity."],"exampleFix":"// before\nvar s = date2str(myDate, 'yyyy~MM~dd?'); // invalid pattern\n// after\nvar s = date2str(myDate, 'yyyy-MM-dd');","handlingStrategy":"validation","validationCode":"if (typeof fmt !== 'string' || fmt.length === 0) throw new Error('date2str format must be a non-empty SimpleDateFormat pattern');\nif (!isJavaDate(d)) throw new Error('first argument must be a java.util.Date');","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); } catch (e) { if (String(e.message).indexOf('Could not convert to the given format') >= 0) { Logger.log('check date type and pattern: ' + fmt); s = date2str(d); } else { throw e; } }","preventionTips":["Validate the pattern is a legal SimpleDateFormat pattern before calling","Ensure the first argument is a Date, converting strings via str2date()","Test the pattern in isolation with a known Date","Avoid exotic pattern letters; stick to documented ones"],"tags":["javascript","date-formatting","type-mismatch","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"}