IF condition tjava

Seven Stars

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"


Accepted Solutions
Ten Stars

Re: IF condition tjava

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

All Replies
Ten Stars

Re: IF condition tjava

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

2019 GARNER MAGIC QUADRANT FOR DATA INTEGRATION TOOL

Talend named a Leader.

Get your copy

OPEN STUDIO FOR DATA INTEGRATION

Kickstart your first data integration and ETL projects.

Download now

What’s New for Talend Summer ’19

Watch the recorded webinar!

Watch Now

Best Practices for Using Context Variables with Talend – Part 2

Part 2 of a series on Context Variables

Blog

Best Practices for Using Context Variables with Talend – Part 1

Learn how to do cool things with Context Variables

Blog

Migrate Data from one Database to another with one Job using the Dynamic Schema

Find out how to migrate from one database to another using the Dynamic schema

Blog