Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Connect 2026 Agenda Now Available: Explore Sessions
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

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 ! 0683p000009MACn.png
Labels (2)
16 Replies
Anonymous
Not applicable
Author

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";
Anonymous
Not applicable
Author

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 ? 0683p000009MACn.png
Anonymous
Not applicable
Author

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 0683p000009MACn.png
Anonymous
Not applicable
Author

See the code I created for this and modify as needed to adding and subtracting on a day instead of a month.
https://community.talend.com/t5/Design-and-Development/TalendDate-Question/td-p/99365, 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
Anonymous
Not applicable
Author

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 ?
Anonymous
Not applicable
Author

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

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 ! 0683p000009MACn.png
Anonymous
Not applicable
Author

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
++
Anonymous
Not applicable
Author

Thank you very much, I've downloaded TOS 3.1.0 and now I can use addDate 0683p000009MACn.png
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 0683p000009MACn.png