I have two connections to different databases. Two dates come from them. Through the tMap component, I want to compare these dates according to the condition that if hive_data > postgres_data or postgres_data is null, run the bash script through the tSystem component.
https://prnt.sc/Th3Cr1PoxNaa - job
https://prnt.sc/95HsXYnjzdyB - tMap expression comparison
If you want to compare date, use TalendDate.compareDate() .
The TalendDate.compareDate function in Talend is used to compare two dates and returns an integer value indicating their relative order. The function takes two arguments, both of which should be in the format of java.util.Date objects. The function will return 0 if the two dates are equal, a positive integer if the first date is after the second date, and a negative integer if the first date is before the second date.
Here is an example of how to use the TalendDate.compareDate function in a Talend Job:
// Define two date variables
java.util.Date date1 = TalendDate.parseDate("yyyy-MM-dd", "2022-01-01");
java.util.Date date2 = TalendDate.parseDate("yyyy-MM-dd", "2022-12-31");
// Compare the two dates
int comparison = TalendDate.compareDate(date1, date2);
// Check the result of the comparison
if (comparison < 0) {
System.out.println(date1 + " is before " + date2);
} else if (comparison == 0) {
System.out.println(date1 + " is equal to " + date2);
} else {
System.out.println(date1 + " is after " + date2);
}
Related
I combine two columns; date and time. When I pass the date and time hot coded it works fine but when I pass it through a column it throws the error:
Unparseable date: "05/05/1992"
I already tried this:
MaterialCodeCSV.xdate == null ?
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")) :
TalendDate.parseDateLocale("yyyy/mm/dd HH:mm:ss",MaterialCodeCSV.xdate.toString() + MaterialCodeCSV.xtime.toString(),"EN");
Java code in Talend:
Date handling can be a bit tricky if using wrong data types. I assume you want to fill a field which is a Date. There are several errors with this way:
MaterialCodeCSV.xdate == null ?
TalendDate.parseDate("yyyy-MM-dd HH:mm:ss", TalendDate.getDate("yyyy-MM-dd HH:mm:ss")) :
TalendDate.parseDateLocale("yyyy/mm/dd H:mm:ss",MaterialCodeCSV.xdate.toString()+ MaterialCodeCSV.xtime.toString(),"EN");
If MaterialCodeCSV.xdate == null you create a date and parse it again instantly? That seems unneccessary complex and inefficient. Change this to TalendDate.getCurrentDate()
Then if xdate is not null, you just concat xdate and xtime, use toString() and try to parse this. Again, this seems unneccesary complex. If I just assume now and xdate and xtime are already Date fields, you could write it as this: MaterialCodeCSV.xdate + MaterialCodeCSV.xtime.
If both are String fields, you have to make sure that xdate is formatted yyyy/MM/dd and xtime is HH:mm:ss. Then you could exclude .toString()
Also, if both are String fields, you have to add an additional space: MaterialCodeCSV.xdate + ' ' + MaterialCodeCSV.xtime
Additionally, in the first case you parse with yyyy-MM-dd HH:mm:ss. In the second case you parse with yyyy/mm/dd H:mm:ss. This reads "year/minute/day". Also there is only one hour digit, not allowing anything after 9:59:59 o'clock to be parsed. Correctly you should use yyyy/MM/dd HH:mm:ss.
So to conclude it should look like this (if I assume correctly and you are using correctly formatted String fields for xdate and xtime):
MaterialCodeCSV.xdate == null ?
TalendDate.getCurrentDate() :
TalendDate.parseDateLocale("yyyy/MM/dd HH:mm:ss", MaterialCodeCSV.xdate + ' ' + MaterialCodeCSV.xtime,"EN");
i need to calculate difference between two date excluding sunday. I have table with dates and i need to calculate number of dates of repeated days from last date.
if i have dates like that
27-05-2017
29-05-2017
30-05-2017
I use this code in script
date(max(Date)) as dateMax,
date(min(Date)) as dateMin
And i get min date = 27-05-2017 and max date = 30-05-2017 then i use in expressions
=floor(((dateMax - dateMin)+1)/7)*6 + mod((dateMax - dateMin)+1,7)
+ if(Weekday(dateMin) + mod((dateMax - dateMin)+1,7) < 7, 0, -1)
And get result 3 days. Thats OK, but the problem is if I have next dates:
10-05-2017
11-05-2017
27-05-2017
29-05-2017
30-05-2017
When use previously code I get min date = 10-05-2017 and max date = 30-05-2017 and result 18, but this is not OK.
I need to count only dates from
27-05-2017
29-05-2017
30-05-2017
I need to get max date and go throw loop repeated dates and if have brake to see is that date sunday if yes then step that date and continue to count repeated dates and if i again have break and if not sunday than close loop and remember number of days.
In my case instead of 18 days i need to get 3 days.
Any idea?
I'd recommend you creating a master calendar in the script where you can apply weights or any other rule to your days. Then in your table or app you can just loop through the dates or perform operations and sum their weights (0: if sunday, 1: if not). Let's see an example:
// In this case I'll do a master calendar of the present year
LET vMinDate = Num(MakeDate(year(today()),1,1));
LET vMaxDate = Num(MakeDate(year(today()),12,31));
Calendar_tmp:
LOAD
$(vMinDate) + Iterno() - 1 as Num,
Date($(vMinDate) + Iterno() - 1) as Date_tmp
AUTOGENERATE 1 WHILE $(vMinDate) + Iterno() - 1 <= $(vMaxDate);
Master_Calendar:
LOAD
Date_tmp AS Date,
Week(Date_tmp) as Week,
Year(Date_tmp) as Year,
Capitalize(Month(Date_tmp)) as Month,
Day(Date_tmp) as Day,
WeekDay(Date_tmp) as WeekDay,
if(WeekDay = '7',0,1) as DayWeight //HERE IS WHERE YOU COULD DEFINE A VARIABLE TO DIRECTLY COUNT THE DAY IF IT IS NOT SUNDAY
'T' & ceil(num(Month(Date_tmp))/3) as Quarter,
'T' & ceil(num(Month(Date_tmp))/3) & '-' & right(year(Date_tmp),2) as QuarterYear,
date(monthStart(Date_tmp),'MMMM-YYYY') as MonthYear,
date(monthstart(Date_tmp),'MMM-YY') as MonthYear2
RESIDENT Calendar_tmp
ORDER BY Date_tmp ASC;
DROP Table Calendar_tmp;
I am having a field which contains date in the form of number and need to convert into equivalent date for further operations such as checking between the date with other date variables.
For example : My long number variable is
Dim ndate as Long
ndate=20140901
I need to get this ndate as date variable such as 01/09/2014 (dd/mm/yyyy)
Thanks in advance
dim actualdate as date
actualdate = dateserial(ndate\10000, (ndate mod 10000)\100, ndate mod 1000000)
DateSerial takes the arguments year, month, day. The \ operator performs division discarding the remainder, and mod performs a division returning the remainder.
This can give you the answer
If the date is 20140901
then newdate will have 01/09/2014
dim newdate as date
newdate = CDate(Right(ndate, 2) & "/" & Mid(ndate, 5, 2) & "/" & Left(ndate, 4))
I have an Access 2013 form which has two unbound date fields, FromDate and ToDate. I insert these into a table (TblGuestBooking) which has an autonumber key field, so this doesn't feature in the SQL statement to follow.
If the FromDate and ToDate are in the same month, the dates are entered as dd/mm/yy, the format of the form field. If, however, the From date is in one month and the to date is in the next month or later month, the format changes to mm/dd/yy for the subsequent months.
for example 26/2/14 to 3/3/14 results in the following table entries:
26/02/14,
27/02/14,
28/02/14,
03/01/14,
03/02/14,
03/03/14
This is the code snippet I am using to put the dates into the table (BookingID is obtained from the form.)
Dim BookingDate As Date
Dim SQLString As String
....
BookingDate = FromDate
Do
SQLString = "INSERT INTO TblGuestBooking ([BookingDate], [BookingID]) VALUES (#" & BookingDate & "#" & "," & Me.GuestSuiteBookingID & ")"
DoCmd.SetWarnings False
DoCmd.RunSQL SQLString
DoCmd.SetWarnings True
BookingDate = BookingDate + 1
Loop Until BookingDate = ToDate + 1
If you've read this far, thank you for your time. If you can help me out, many, many thanks.
When processing date literals (text values enclosed by "hash marks" #) Access SQL will always interpret ambiguous xx-yy-zzzz dates as mm-dd-yyyy, regardless of the regional setting in place on the computer. So, if your machine is configured to display short dates as dd-mm-yyyy and you create an Access query that uses #04-02-2014# it will always be interpreted as April 2, not February 4.
The solution is to always format date literals as unambiguous yyyy-mm-dd values. In your case, instead of
... VALUES (#" & BookingDate & "#" ...
you would use something like
... VALUES (#" & Format(BookingDate, "yyyy-mm-dd") & "#" ...
In my case I've used following code for an ASP page that create and update a Date field in Access database.
strDay = Request("Day")
strMonth = Request("Month")
strYear = Request("Year")
InputDate = strYear & "-" & strMonth & "-" & strDay
if IsDate(InputDate) Then
InsertDate = CDate(InputDate)
else
'if date typed wrong, use Todays Date
InsertDate = Date()
end if
SQLUPD = "UPDATE MyDataBase SET DateField = '"& FormatDateTime(InsertDate, vbShortDate) &"'"
SQLINS = "INSERT INTO MyDataBase (DateField, Alist, Atype, Acomment) VALUES ('"& FormatDateTime(InsertDate, vbShortDate) &"', .....
Then all sorting and output opration with dates are going well.
XQuery has a set of useful functions for date conversion. But how to convert relative dates like "Today" and "Yesterday" into the actual date?
For example "Today, 17:33" should be converted to "2012-05-30", and "Yesterday, 22:13" to "2012-05-29".
1. Parse the Date string
Parse the date string. I provided a small function which splits the word indicating the date off and parses it. I added some more convenient names for dates, but you can easily add more if neccessary, notice I convert to lower-case! It uses XQuery date and time functions for calculating the matching date.
declare function local:from-relative-date($string as xs:string) as xs:date {
switch (lower-case(substring-before($string, ",")))
case "today" return current-date()
case "yesterday" return current-date() - xs:dayTimeDuration('P1D')
case "day before yesterday" return current-date() - 2 * xs:dayTimeDuration('P1D')
case "tomorrow" return current-date() + xs:dayTimeDuration('P1D')
case "day after tomorrow" return current-date() + 2 * xs:dayTimeDuration('P1D')
default return error(xs:QName("XPTY0004"), "Unknown Date")
};
2. Format the date
Now use the XQuery 3.0 function format-date(...) (I hope your XQuery engine supports it, BaseX which I used for this example does) to format the date string like you need it:
format-date(local:from-relative-date("Yesterday, 22:13"), "[Y]-[M00]-[D00]")
If it doesn't, you will have to use year-from-date(...) and according functions for month and day, use some number formatting and concatenate yourself.