Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
daez
Creator
Creator

IF condition tjava

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 ?

 

if ( ((String)globalMap.get("tFileList_1_CURRENT_FILE")).equals("AA_MAGAS_*") )

{
	
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"

Labels (3)
1 Solution

Accepted Solutions
Jesperrekuh
Specialist
Specialist

import java.util.regex.*;

String fname1= "AAA_BBBB_CCC_DDD_20180101_1233.xml";
String fname2= "AAA_20010101_0833_foo.xml";
Matcher m1 = Pattern.compile(".*([0-9]{8}_[0-9]{4}).*").matcher(fname1);
Matcher m2 = Pattern.compile(".*([0-9]{8}_[0-9]{4}).*").matcher(fname2);
if(m1.matches() && m2.matches())
{
    System.out.println("dt file1: " + m1.group(1));
    System.out.println("dt file2: " + m2.group(1));
}

View solution in original post

1 Reply
Jesperrekuh
Specialist
Specialist

import java.util.regex.*;

String fname1= "AAA_BBBB_CCC_DDD_20180101_1233.xml";
String fname2= "AAA_20010101_0833_foo.xml";
Matcher m1 = Pattern.compile(".*([0-9]{8}_[0-9]{4}).*").matcher(fname1);
Matcher m2 = Pattern.compile(".*([0-9]{8}_[0-9]{4}).*").matcher(fname2);
if(m1.matches() && m2.matches())
{
    System.out.println("dt file1: " + m1.group(1));
    System.out.println("dt file2: " + m2.group(1));
}