Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi,
I want to get date as my context parameter, but when I do this, it changes it's format. e.g. my date is 07-17-2019, it changes this to Jul 17, 2019. May be this is because context date type doesn't have format in context parameter, can someone suggest how to achieve this. For the time, I am having all parameters in string and later casting it to date.
Hello,
you cannot have a Date in a context variable a print it as a String without converting it to a String first.
This conversion can either be done explicitly (manually) with the TalendDate.formatDate() method in which you can specify a format.
Or the conversion can be done implicitly where you will not be able to control the format. This is what happen when you use stuff like:
System.out.println(context.myDate); //or String filename="myFilename_" + context.myDate + ".txt";
The correct way to do what you want is to use a Date in the context variable and explicitly (manually) convert it whenever you want to use it in a String (be it a filename or a log or whatever).
System.out.println(TalendDate.formatDate("yyyy-MM-dd HH:mm:ss", context.myDate)); //or String filename="myFilename_" + TalendDate.formatDate("yyyyMMdd'T'HHmmss", context.myDate) + ".txt";
If you have to change the date, I would also recommand to use a global variable to store the modified date and work on it:
globalMap.put("myWrkDate", context.myDate); //add 12 days globalMap.put("myWrkDate", TalendDate.addDate((Date)globalMap.get("myWrkDate"), 12, "dd")); //use this variable in a filename String filename="text_" + TalendDate.formatDate("yyyyMMdd'T'HHmmss", (Date)globalMap.get("myWrkDate")) + ".txt";
Of course, those are simple examples you'll have to adapt to your needs and jobs.
You can also create a routine if you really have to use this in a lot of places.
Regards