yesterday's date

One Star

yesterday's date

Hello !
I'm using a variable nb_day which is equal to 0 for today, 1 for yesterday, 2 for the day before...
I'm creating files and I want the date to be in the file names, so that for today my file name would be :
"D:/talend/AAA_"+TalendDate.formatDate("yyyyMMdd",TalendDate.getCurrentDate())+".log"
How can I get yesterday's date and so on like I do for today and using my variable ?
(basically I'm trying to do "D:/talend/AAA_"+TalendDate.formatDate("yyyyMMdd",TalendDate.getCurrentDate() - nb_day)+".log" but this is not working)
Thanks for your help ! Smiley Happy
One Star

Re: yesterday's date

Use the "addDate" method :
Date today = TalendDate.getCurrentDate();
Date wantedDay = TalendDate.addDate(today, - nb_day, "dd");
String logFileName = "D:/talend/AAA_" + TalendDate.formatDate("yyyyMMdd", wantedDay) + ".log";
This code can be written in a single line :
String logFileName = "D:/talend/AAA_" + TalendDate.formatDate("yyyyMMdd", TalendDate.addDate(TalendDate.getCurrentDate(), - nb_day, "dd")) + ".log";
One Star

Re: yesterday's date

Thank bcourtine !
However I get this error message :
The method addDate(Date, int, String) is undefined for the type TalendDate.
Any idea what I can do ? Smiley Happy
Seventeen Stars

Re: yesterday's date

hi all,
Have you got public static Date addDate(Date date, int nb, String dateType) method in Routines TalendDate !?
because jvm seems dont' fnd it ... (bizarre Smiley Happy
One Star

Re: yesterday's date

See the code I created for this and modify as needed to adding and subtracting on a day instead of a month.
http://www.talendforge.org/forum/viewtopic.php?id=6497, Here's the original code which gets the last day of the month for any month past or future.

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);
}
}
Yours should look like this,
public static Date getYesterDaysDate(String pattern, int DayAdd) {
Calendar now = Calendar.getInstance();
now = Calendar.getInstance();
now.add(Calendar.DATE, DaYAdd);
String MyDate = (now.get(Calendar.YEAR) "-"
String.format("%02d",(now.get(Calendar.MONTH))) "-"
now.get(Calendar.DATE));
try {
return FastDateParser.getInstance(pattern).parse(MyDate);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}
You would create your own code routine called something like YesterDaysDate and call this like this
YesterDaysDate.getYesterDaysDate("yyyy-MM-dd", -1)
That should do it, look at the TalendDate code for the imports...
Bluesin
One Star

Re: yesterday's date

I think the easiest way for me to do what I need is indeed to use addDate... where do I find this method and how do I add it to the routines ?
One Star

Re: yesterday's date

Hi Augustine,
I think that the TalendDate.addDate method wasn't available before TOS 3.1.0.
Which version do you have ?
One Star

Re: yesterday's date

I'm using version 3.0.3 but if you tell me that addDate is on TOS 3.1, I'll download the new version.
I hope it'll work, thank you ! Smiley Happy
Seventeen Stars

Re: yesterday's date

I think you're right kboop ... I 've a look in routine system of 3.0 ..and it wasn't there !
So Augustine you've to upload to 3.1 but if you don't here the code to add to routines system :
package routines;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import routines.system.FastDateParser;
import routines.system.LocaleProvider;
.........
// code you have to add !!!
/**
* add number of day, month ... to a date
*
* @param date (a <code>Date</code> type value)
* @param nb (the value to add)
* @param dateType (date pattern = ("yyyy","MM","dd","HH","mm","ss","SSS" ))
* @return a new date
*
* {talendTypes} Date
*
* {Category} TalendDate
*
* {param} date(myDate) date : the date to update
*
* {param} date(addValue) nb : the added value
*
* {param} date("MM") dateType : the part to add
*
* {examples}
*
* ->> addDate(2008/11/24 12:15:25, 5,"dd") return 2008/11/29 12:15:25
*
* ->> addDate(2008/11/24 12:15:25, 5,"yyyy")return 2013/11/25 12:15:25
*
* ->> addDate(2008/11/24 12:15:25, 5,"MM") return 2009/02/24 12:15:25
*
* ->> addDate(2008/11/24 12:15:25, 5,"ss") return 2008/11/24 12:15:30 #
*
*/
public static Date addDate(Date date, int nb, String dateType) {
if (date == null || dateType == null) {
return null;
}
Calendar c1 = Calendar.getInstance();
c1.setTime(date);
if (dateType.equalsIgnoreCase("yyyy")) { //$NON-NLS-1$
c1.add(Calendar.YEAR, nb);
} else if (dateType.equals("MM")) { //$NON-NLS-1$
c1.add(Calendar.MONTH, nb);
} else if (dateType.equalsIgnoreCase("dd")) { //$NON-NLS-1$
c1.add(Calendar.DAY_OF_MONTH, nb);
} else if (dateType.equals("HH")) { //$NON-NLS-1$
c1.add(Calendar.HOUR, nb);
} else if (dateType.equals("mm")) { //$NON-NLS-1$
c1.add(Calendar.MINUTE, nb);
} else if (dateType.equalsIgnoreCase("ss")) { //$NON-NLS-1$
c1.add(Calendar.SECOND, nb);
} else if (dateType.equalsIgnoreCase("SSS")) { //$NON-NLS-1$
c1.add(Calendar.MILLISECOND, nb);
} else {
throw new RuntimeException("Can't support the dateType: " + dateType);
}
return c1.getTime();
}

add this code in Code -> routine -> system -> TalendDate
++
One Star

Re: yesterday's date

Thank you very much, I've downloaded TOS 3.1.0 and now I can use addDate Smiley Happy
Just one thing though :
At first I thought I would do something like this :
"D:/talend/AAA_" + TalendDate.formatDate("yyyyMMdd", TalendDate.addDate(TalendDate.getCurrentDate(), -context.nb_days, "dd")) + ".csv"
with nb_days being an integer, but this throws me a NullPointerException, so that I have to write the number of days directly in the file name.
"D:/talend/AAA_" + TalendDate.formatDate("yyyyMMdd", TalendDate.addDate(TalendDate.getCurrentDate(), -1, "dd")) + ".csv"
and this way it's working.
Well it's not a big deal, I'm just wondering whether it can be done or not Smiley Happy
One Star

Re: yesterday's date

It's because you have to multiply your parameter by one instead of just add a minus sign in front of it :
-1 * context.nb_days should work Smiley Wink
Regards,
Karine
One Star

Re: yesterday's date

-1 * context.nb_days should work Smiley Wink

Thanks for your answer ! Unfortunately it seems it doesn't work.
I'm still getting the NullPointerException.
Community Manager

Re: yesterday's date

Hello Augustine
Set a default value to context.nb_days, maybe 1.Smiley Happy
Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: yesterday's date

I'm using v3.0.4 and am not able to modify the TalendDate system routine. Are there any special things I need to do to be able to modify it? Thanks.
Community Manager

Re: yesterday's date

Hello
I'm using v3.0.4 and am not able to modify the TalendDate system routine. Are there any special things I need to do to be able to modify it? Thanks.

I don't think it is a good idea to modify the system routine directly. If you want to enhance it ,you can create a new routine, copy the code and modify it.
Best regards
shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: yesterday's date

String logFileName = "D:/talend/AAA_" + TalendDate.formatDate("yyyyMMdd", TalendDate.addDate(TalendDate.getCurrentDate(), - nb_day, "dd")) + ".log";
Six Stars

Re: yesterday's date

Thanks a lot. I did something similar. 

 

"F:/Talend/Work" + TalendDate.formatDate("MMddyyyy",TalendDate.addDate(TalendDate.getCurrentDate(), - 10, "dd"))+".csv"