pentaho/pentaho-kettle · error · RuntimeException

Could not convert to the given format.

Error message

Could not convert to the given format.

What it means

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.

Solutions

  1. Verify the first argument is a java.util.Date; convert with str2date() if it is a string.
  2. Check the format string is a valid SimpleDateFormat pattern (e.g. 'yyyy-MM-dd HH:mm:ss').
  3. Guard null/non-date values before calling.
  4. Test the pattern in plain Java or a JS snippet to confirm validity.

Example fix

// before
var s = date2str(myDate, 'yyyy~MM~dd?'); // invalid pattern
// after
var s = date2str(myDate, 'yyyy-MM-dd');
Defensive patterns

Strategy: validation

Validate before calling

if (typeof fmt !== 'string' || fmt.length === 0) throw new Error('date2str format must be a non-empty SimpleDateFormat pattern');
if (!isJavaDate(d)) throw new Error('first argument must be a java.util.Date');

Type guard

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

Try / catch

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

Prevention

When it happens

Trigger: date2str(date, pattern) where ArgList[0] is not a java.util.Date (ClassCastException) or SimpleDateFormat rejects the pattern (IllegalArgumentException, e.g. malformed pattern).

Common situations: Passing a string/number instead of a Date; a garbled or empty format string; quotes or invalid pattern letters in the format.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

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

          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:
        try {
          if ( isNull( ArgList, new int[] { 0, 1, 2 } ) ) {
            return null;
          } else if ( isUndefined( ArgList, new int[] { 0, 1, 2 } ) ) {
            return undefinedValue;
          }
          java.util.Date dArg1 = (java.util.Date) ArgList[0];
          DateFormat dfFormatter;
          String sArg2 = (String) ArgList[1];
          String sArg3 = (String) ArgList[2];
          if ( sArg3.length() == 2 ) {
            Locale dfLocale = EnvUtil.createLocale( sArg3.toLowerCase() );
            dfFormatter = new SimpleDateFormat( sArg2, dfLocale );
            oRC = dfFormatter.format( dArg1 );
          } else {

View on GitHub (pinned to f3058517a1)