{"record":{"id":"97ab047a66063912","repo":"pentaho/pentaho-kettle","slug":"could-not-convert-to-local-format","errorCode":null,"errorMessage":"Could not convert to local format.","messagePattern":"Could not convert to local format\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java","lineNumber":1107,"sourceCode":"    Object oRC = new Object();\n    switch ( ArgList.length ) {\n      case 0:\n        throw new RuntimeException( \"Please provide a valid date to the function call date2str.\" );\n      case 1:\n        try {\n          if ( isNull( ArgList ) ) {\n            return null;\n          } else if ( isUndefined( ArgList ) ) {\n            return undefinedValue;\n          }\n          java.util.Date dArg1 = (java.util.Date) ArgList[0];\n          if ( dArg1.equals( null ) ) {\n            return null;\n          }\n          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:","sourceCodeStart":1089,"sourceCodeEnd":1125,"githubUrl":"https://github.com/pentaho/pentaho-kettle/blob/f3058517a153da500bf4551f46d79b91bf8ec552/engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java#L1089-L1125","documentation":"Catch-all rethrow for the 1-argument date2str form: any exception while formatting the date with the default SimpleDateFormat is wrapped into 'Could not convert to local format.'. In practice this fires when ArgList[0] is not a java.util.Date and the cast/format fails (e.g. a string or number is passed instead of a Date).","triggerScenarios":"date2str(x) where x is a String, long, or other non-Date object; the cast/ClassCastException or formatting failure is caught and rethrown as this RuntimeException.","commonSituations":"Passing a date stored as a string in a field; passing a numeric timestamp instead of a Date; a JavaScript Date versus java.util.Date mismatch in the Rhino environment.","solutions":["Ensure the argument is a real java.util.Date (use str2date() to convert strings first, or new Date()).","Convert string inputs explicitly: date2str(str2date(s,'yyyy-MM-dd')).","Check field types coming from previous transformation steps.","Wrap in try/catch to log typeof the argument when debugging."],"exampleFix":"// before\nvar s = date2str('2024-01-01'); // String, not Date\n// after\nvar s = date2str(str2date('2024-01-01', 'yyyy-MM-dd'));","handlingStrategy":"type-guard","validationCode":"if (!(d instanceof Date) && (typeof d !== 'object')) throw new Error('date2str expects a java.util.Date, got: ' + typeof d);","typeGuard":"function isJavaDate(v) { return v != null && typeof v === 'object' && typeof v.getTime === 'function' && v.getClass && String(v.getClass()).indexOf('java.util.Date') >= 0; }","tryCatchPattern":"try { var s = date2str(d); } catch (e) { if (String(e.message).indexOf('Could not convert to local format') >= 0) { d = new Date(d); s = date2str(d); } else { throw e; } }","preventionTips":["Pass java.util.Date values, not strings or numbers","Use str2date() to convert strings before date2str","Check the field type coming from prior transformation steps","Prefer a fallback conversion (new Date(x)) in catch"],"tags":["javascript","type-mismatch","date-formatting","scripting"],"backgroundTag":"type-mismatch","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"}