One Star

[resolved] How to Receive a Date context parameter through the shell script?

If I have a context parameter named "id" of type Integer, I can set it from the command line in the following manner:
Test_0.1.sh --context_param id=100
How would I go about passing a date through the shell script using the --context_param argument? Does Talend have a particular format string it would like to see?
Using a context parameter named "dt" of type Date, I tried the following:
Test_0.1.sh --context_param dt=2014-10-15
But in my program I see that it is null.
Regards,
Matthew
1 ACCEPTED SOLUTION

Accepted Solutions
Seventeen Stars

Re: [resolved] How to Receive a Date context parameter through the shell script?

got it Smiley Happy
trying to put a default date to my context give me the clue .

you can put a "date' ( not really Date type ) with his pattern in the java commandline : (something like)

C:\Talend\test\boolParam_0.1\boolParam>boolParam_run.bat --context_param datej="
yyyy-MM-dd HH:mm:ss;2014-10-16 12:18:33"

So you can Smiley Wink
regards
laurent
8 REPLIES
Seventeen Stars

Re: [resolved] How to Receive a Date context parameter through the shell script?

hi mmoisen,
you are not 'passing a date' to your script.
it's just a suite of caraters that will be be seen as a String in java. So if you have to manage Date type in your Talend Process, you'll have to convert string in Date before.
I often read a date as a string until I have to use Date methods to do some calculation (ex : add a date). Otherwise keep it in String.

TalendDate.parseDate("dd-MM-yyy",context.dateparam)

regards
laurent
One Star

Re: [resolved] How to Receive a Date context parameter through the shell script?

hi mmoisen,
you are not 'passing a date' to your script.
it's just a suite of caraters that will be be seen as a String in java. So if you have to manage Date type in your Talend Process, you'll have to convert string in Date before.
I often read a date as a string until I have to use Date methods to do some calculation (ex : add a date). Otherwise keep it in String.

TalendDate.parseDate("dd-MM-yyy",context.dateparam)

regards
laurent

Hi Laurent,
In the case of a context variable I've defined as a Long or Integer, Talend apparently converts it for me into the correct data type. However for Date your conclusion is correct as far as I can tell-- I'll convert it into a Date before utilizing it.
Thanks,
Matthew
Seventeen Stars

Re: [resolved] How to Receive a Date context parameter through the shell script?

I've defined as a Long or Integer, Talend apparently converts it for me into the correct data type

??? Smiley Happy
if you have define your data as Long or Integer, you have to convert explicitly it into Date to have a Date, no ?
otherwise you'll have
Type mismatch: cannot convert from Integer to Date
One Star

Re: [resolved] How to Receive a Date context parameter through the shell script?

I mean to say, if I've defined a context variable as a Long, and I pass the value via test_1.0.sh --context_param long_id=123, Talend appears to convert it from Long to String for me.
But if I've defined a context variable as a Date, it won't convert from String to Date for me.
Seventeen Stars

Re: [resolved] How to Receive a Date context parameter through the shell script?

but in '--context_param long_id=123', 123 is not a String or a Long or any other java type Smiley Wink
outside java (talend) you are not manipulating java type.
a value for a param context , coming from shell script or a config.ini will always deen as a String (java type) in your application.
One Star

Re: [resolved] How to Receive a Date context parameter through the shell script?

but in '--context_param long_id=123', 123 is not a String or a Long or any other java type Smiley Wink
outside java (talend) you are not manipulating java type.
a value for a param context , coming from shell script or a config.ini will always deen as a String (java type) in your application.

Hey Kzone,
I've tried it with Long, Integer, and Boolean. Talend must be smart enough to convert it from a String to the appropriate java type.
For example, I have a job called TestCommandLineBoolean, with a context variable named "is_bool" of type Boolean

and one tJava with the following code:

if (context.is_bool != null) {
    if (context.is_bool) {
        System.out.println("is_bool is true");
    } else {
        System.out.println("is_bool is false");
    }
} else if (context.is_bool == null) {
    System.out.println("is_bool is null");
} else {
    System.out.println("is_bool is neither!");
}

If I run it without passing a context parameter, is_bool defaults to false:

If I run it passing in "true", "True", or "TrUe", is_bool is set to true:



However, If I run it passing in "truetrue", "false", or "abcd", is_bool is set to (or remains) false:



So Talend appears to be converting it for me.
I would be curious to know if it also does the same for Dates, given it is entered in the proper format.
Regards,
Matthew
Seventeen Stars

Re: [resolved] How to Receive a Date context parameter through the shell script?

you're right for boolean

try {
                context.chooce = routines.system.ParserUtils
                        .parseTo_Boolean(context.getProperty("chooce"));

as declared as Boolean, Talend try to parse context flow (String) in a boolean.
Assign null at exception :

catch (NumberFormatException e) {
                context.chooce = null;
            }
Seventeen Stars

Re: [resolved] How to Receive a Date context parameter through the shell script?

got it Smiley Happy
trying to put a default date to my context give me the clue .

you can put a "date' ( not really Date type ) with his pattern in the java commandline : (something like)

C:\Talend\test\boolParam_0.1\boolParam>boolParam_run.bat --context_param datej="
yyyy-MM-dd HH:mm:ss;2014-10-16 12:18:33"

So you can Smiley Wink
regards
laurent