Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
daez
Creator
Creator

tJava - IF ELSE statement fails!

Hello everyone, I'm trying to compare filename with date inside to whether or not delete them.

Since I have 2 types of filesname : "AA_MAGAS_yyyyMMdd_hhmm" and "AA_BBB_CCC_yyyyMMdd_hhmm" , my idea was to substring the filename (from 9 to 22 for first file type and 11 to 24 for the second one), get the date out & compare it. Then use this result in a run if condition to delete it.

My job was working for only 1 file type, but since I tried a IF condition to deal with the 2 filesname, its not working anymore. Can someone have a check ?

 

{
	
context.strdate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(9,22);

context.date = TalendDate.parseDate("yyyyMMdd_hhmm",context.strdate);

//difference of days between current date & file date, >15 = delete (runif)

context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd");

}

else {
	

context.ecodate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(11,24);

context.date = TalendDate.parseDate("yyyyMMdd_hhmm", context.ecodate);

context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd");


};


My error, seems like the IF condition is not working because the substring get it wrong :
java.lang.RuntimeException: java.text.ParseException: Unparseable date: "180210_0345.x"

 

Thanks !

Labels (3)
1 Solution

Accepted Solutions
Anonymous
Not applicable

This is caused by your wildcard. You cannot use a wildcard in Equals.

Try this....

 

if ( ((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf("AA_MAGAS_")>-1 )

{
	
context.strdate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(9,22);

context.date = TalendDate.parseDate("yyyyMMdd_hhmm",context.strdate);

//difference of days between current date & file date, >15 = delete (runif)

context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd");

}

else {
	

context.ecodate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(11,24);

context.date = TalendDate.parseDate("yyyyMMdd_hhmm", context.ecodate);

context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd");


};

You may also need to account for NULL if the filename can be empty. I've not done that, but you should think about it.

View solution in original post

2 Replies
Anonymous
Not applicable

This is caused by your wildcard. You cannot use a wildcard in Equals.

Try this....

 

if ( ((String)globalMap.get("tFileList_1_CURRENT_FILE")).indexOf("AA_MAGAS_")>-1 )

{
	
context.strdate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(9,22);

context.date = TalendDate.parseDate("yyyyMMdd_hhmm",context.strdate);

//difference of days between current date & file date, >15 = delete (runif)

context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd");

}

else {
	

context.ecodate = ((String)globalMap.get("tFileList_1_CURRENT_FILE")).substring(11,24);

context.date = TalendDate.parseDate("yyyyMMdd_hhmm", context.ecodate);

context.difference = TalendDate.diffDate(TalendDate.getCurrentDate(),context.date,"dd");


};

You may also need to account for NULL if the filename can be empty. I've not done that, but you should think about it.

David_Beaty
Specialist
Specialist

Hi,

 

Also, as you're parsing a date from part of a filename, you should consider using the TalendDate.isDate() function, and only attempt the parseDate if it is a date.

 

Thanks

 

David