TalendDate Question.

One Star

TalendDate Question.

Hi I'd like to add a date field to my data that basically contains the date for the first day of the month, is there a way to use talenddate to extract the current month and year from the current date and then format a date field using the extracted data as in "extractedyear, extracted month, 01".
Thanks,
Bluesin
One Star

Re: TalendDate Question.

Will this work?
In TMAP I added the field date and then opened up the formula editor and put this in,
DateSerial(Year(Date), Month(Date) , 1)
Will that do it?
Thanks,
Bluesin
One Star

Re: TalendDate Question.

Oops, sorry the above came from an ASP page that I thought was Java as I did a search on Java Date...
My BadSmiley Embarassed)
Bluesin
One Star

Re: TalendDate Question.

Additionally on this. When I'm using he formula editor what is the construct used within. I found this code to get the last date of the month,
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
int lastDate = calendar.getActualMaximum(Calendar.DATE);
System.out.println("Date : " + calendar.getTime());
System.out.println("Last Date: " + lastDate);
}
}
So if I want to use the calendar class within the formula editor do I have to import it as in the above, do I format the code the same way or do I need to make this a separate subroutine such as talenddate is?
Thanks,
Bluesin
One Star

Re: TalendDate Question.

Ok, so I created a routine to return the date for the last day of the month for the previous month, this is it,
public static String getDate() {
Calendar now = Calendar.getInstance();
now = Calendar.getInstance();
now.add(Calendar.MONTH, -0);
return (now.get(Calendar.YEAR) + "-"
+ now.get(Calendar.MONTH) + "-"
+now.getActualMaximum(Calendar.DATE));
I'd like to return it as a date, I am guessing I can copy the code for parsing the date from Talenddate to return it as a date, but if anyone has any other suggestions then please feel free...
Bluesin
Community Manager

Re: TalendDate Question.

Hello Bluesin
If you return a String, you can use TalendDate to convert it to a Date on tMap, for example:
TalendDate.parseDate("yyyy-MM-dd", yourRoutineName.getDate())

Best regards

shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: TalendDate Question.

Thanks Shong, I thought about that however I figured I'd just do it all in my routine. Here's the whole routine for anyone who migh need such code, this will produce the last day of the month for any month based on what is passed to it.
public static Date getLastDate(String pattern, int MonthAdd) {
Calendar now = Calendar.getInstance();
now = Calendar.getInstance();
now.add(Calendar.MONTH, MonthAdd);
String MyDate = (now.get(Calendar.YEAR) + "-"
+ String.format("%02d",(now.get(Calendar.MONTH) + 1)) + "-"
+now.getActualMaximum(Calendar.DATE));
try {
return FastDateParser.getInstance(pattern).parse(MyDate);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
Its called like this from within TMAP
LastDayOfMonth.getLastDate("yyyy-MM-dd", -1)
Pretty cool, and a kudos to Talend for making the IDE so user friendly for a java newbie like me!
Thanks,
Bluesin