pentaho/pentaho-kettle · error · RuntimeException

Could not convert to local format.

Error message

Could not convert to local format.

What it means

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).

Solutions

  1. Ensure the argument is a real java.util.Date (use str2date() to convert strings first, or new Date()).
  2. Convert string inputs explicitly: date2str(str2date(s,'yyyy-MM-dd')).
  3. Check field types coming from previous transformation steps.
  4. Wrap in try/catch to log typeof the argument when debugging.

Example fix

// before
var s = date2str('2024-01-01'); // String, not Date
// after
var s = date2str(str2date('2024-01-01', 'yyyy-MM-dd'));
Defensive patterns

Strategy: type-guard

Validate before calling

if (!(d instanceof Date) && (typeof d !== 'object')) throw new Error('date2str expects a java.util.Date, got: ' + typeof d);

Type guard

function isJavaDate(v) { return v != null && typeof v === 'object' && typeof v.getTime === 'function' && v.getClass && String(v.getClass()).indexOf('java.util.Date') >= 0; }

Try / catch

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; } }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of pentaho/pentaho-kettle@f3058517a1 (2026-09-13). Data as JSON: /api/errors/97ab047a66063912. Report an issue: GitHub.

Appendix: source

Thrown at engine/src/main/java/org/pentaho/di/trans/steps/script/ScriptAddedFunctions.java:1107

    Object oRC = new Object();
    switch ( ArgList.length ) {
      case 0:
        throw new RuntimeException( "Please provide a valid date to the function call date2str." );
      case 1:
        try {
          if ( isNull( ArgList ) ) {
            return null;
          } else if ( isUndefined( ArgList ) ) {
            return undefinedValue;
          }
          java.util.Date dArg1 = (java.util.Date) ArgList[0];
          if ( dArg1.equals( null ) ) {
            return null;
          }
          Format dfFormatter = new SimpleDateFormat();
          oRC = dfFormatter.format( dArg1 );
        } catch ( Exception e ) {
          throw new RuntimeException( "Could not convert to local format." );
        }
        break;
      case 2:
        try {
          if ( isNull( ArgList, new int[] { 0, 1 } ) ) {
            return null;
          } else if ( isUndefined( ArgList, new int[] { 0, 1 } ) ) {
            return undefinedValue;
          }
          java.util.Date dArg1 = (java.util.Date) ArgList[0];
          String sArg2 = (String) ArgList[1];
          Format dfFormatter = new SimpleDateFormat( sArg2 );
          oRC = dfFormatter.format( dArg1 );
        } catch ( Exception e ) {
          throw new RuntimeException( "Could not convert to the given format." );
        }
        break;
      case 3:

View on GitHub (pinned to f3058517a1)