Date Routine

Two Stars

Date Routine

Hi! I have some trouble with Java routines in Talend. I need to generate some routine which calculate a quarter of the year to load a time dimension. Then, I use it in tmap component.
In java code, would be like this:
switch (mes)
case 1
case 2
case 3
quarter=1
break;
case 4
case 5
case 6
quarter=2
break;
case 7
case 8
case 9
quarter=3
break;
case 10
case 11
case 12
quarter=4
break;
I have a date variable called "fecha", who maps to another component tmap. There I use "fecha" to generate a kind of time attributes; one of this is quarter.
My question is...how I must take this variable "fecha" in java routine to obtain month and make the code?. Date is in this format ("dd-MM-yyyy")
And then, how I use this routine in tmap component?
The picture shows date variable in tmap
One Star

Re: Date Routine

Hi,
The month of your date field can determined by using
new SimpleDateFormat("MM").format(row2.fecha)
You can use this as input to your switch statement.
Hope this helps.
Regards,
Arno
Seven Stars

Re: Date Routine

Just use the following expression to get the quarter of fecha:
TalendDate.getPartOfDate("MONTH",row2.fecha)/3+1
Seven Stars

Re: Date Routine

Two Stars

Re: Date Routine

Hi! Thank you so much for your replies of this topic.
I found a solution like this:
1)First I used a variable in tmap component called mes
mes=Mathematical.INT(TalendDate.formatDate("MM",row2.fecha))
2) In the same way, I use another variable called anio.
anio= TalendDate.formatDate("yyyy", row2.fecha)
3) Then I used both variables and concat because I needed some like this: 20123, where 2012 is the year, and 3 the quarter of year.
Var.mes<=3?Var.anio.concat("1"):Var.mes<=6?Var.anio.concat("2"):Var.mes<=9?Var.anio.concat("3"):Var.anio.concat("4");
I attach some pictures
Regards and thak you so much!!