Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I'm wondering if anyone has run across this behavior or if I am somehow doing something wrong. If I try to format a date with TalendDate.dateFormat("YYYY-MM-dd HH:mm:ss", myDate), everything seems to work fine UNLESS the HH is 12 in which case the output is 00:mm:ss.
I am running Talend Studio 7.3.1 with patch R2023-01. The code I used for the testing (all in a tJava component) is:
String dateStr;
String yearToSecFmt = "YYYYMMddHHmmss";
String yearToSecTimeFmt = "YYYY-MM-dd HH:mm:ss";
Date theDate;
Integer timeBase = 2023013000;
Integer currTime;
System.out.println("======================================================================================================");
System.out.println("*****************************************");
for (int h = 0; h < 24 ; h++)
{
currTime = timeBase + h;
dateStr = currTime.toString() + "0346";
theDate = TalendDate.TO_DATE(dateStr, yearToSecFmt);
System.out.println("Hour = " + Integer.toString(h));
System.out.println("Original = " + dateStr);
System.out.println("My Time = " + TalendDate.formatDate(yearToSecTimeFmt, theDate));
System.out.println("*****************************************");
}
The output from this is (note the hour = 12 output has 00 for the hour, not 12, but hour = 13 has 13 for the hour and not 01):
======================================================================================================
*****************************************
Hour = 0
Original = 20230130000346
My Time = 2023-01-30 00:03:46
*****************************************
Hour = 1
Original = 20230130010346
My Time = 2023-01-30 01:03:46
*****************************************
Hour = 2
Original = 20230130020346
My Time = 2023-01-30 02:03:46
*****************************************
Hour = 3
Original = 20230130030346
My Time = 2023-01-30 03:03:46
*****************************************
Hour = 4
Original = 20230130040346
My Time = 2023-01-30 04:03:46
*****************************************
Hour = 5
Original = 20230130050346
My Time = 2023-01-30 05:03:46
*****************************************
Hour = 6
Original = 20230130060346
My Time = 2023-01-30 06:03:46
*****************************************
Hour = 7
Original = 20230130070346
My Time = 2023-01-30 07:03:46
*****************************************
Hour = 8
Original = 20230130080346
My Time = 2023-01-30 08:03:46
*****************************************
Hour = 9
Original = 20230130090346
My Time = 2023-01-30 09:03:46
*****************************************
Hour = 10
Original = 20230130100346
My Time = 2023-01-30 10:03:46
*****************************************
Hour = 11
Original = 20230130110346
My Time = 2023-01-30 11:03:46
*****************************************
Hour = 12
Original = 20230130120346
My Time = 2023-01-30 00:03:46
*****************************************
Hour = 13
Original = 20230130130346
My Time = 2023-01-30 13:03:46
*****************************************
Hour = 14
Original = 20230130140346
My Time = 2023-01-30 14:03:46
*****************************************
Hour = 15
Original = 20230130150346
My Time = 2023-01-30 15:03:46
*****************************************
Hour = 16
Original = 20230130160346
My Time = 2023-01-30 16:03:46
*****************************************
Hour = 17
Original = 20230130170346
My Time = 2023-01-30 17:03:46
*****************************************
Hour = 18
Original = 20230130180346
My Time = 2023-01-30 18:03:46
*****************************************
Hour = 19
Original = 20230130190346
My Time = 2023-01-30 19:03:46
*****************************************
Hour = 20
Original = 20230130200346
My Time = 2023-01-30 20:03:46
*****************************************
Hour = 21
Original = 20230130210346
My Time = 2023-01-30 21:03:46
*****************************************
Hour = 22
Original = 20230130220346
My Time = 2023-01-30 22:03:46
*****************************************
Hour = 23
Original = 20230130230346
My Time = 2023-01-30 23:03:46
*****************************************
======================================================================================================
Does anyone know if this is a bug in Talend/TalendDate or what I'm doing wrong, if this is not a bug.
EDIT: This came up in a process where I'm loading data into a database and I'm converting the date time to a DB-compliant format. As this is a somewhat urgent issue for me, I'm going to rework the process to have the DB convert the date string to a DB compliant version but I would like to understand what, if anything, I'm doing wrong.
Hello @Bill Gualtiere ,
I can reproduce the issue in my side. there is some issue about the TalendDate.dateFormat("YYYY-MM-dd HH:mm:ss", myDate)
As instead, please try the workaround by using SimpleDateFormat directly like
SimpleDateFormat yearToSecFmt = new SimpleDateFormat("YYYYMMddHHmmss");
SimpleDateFormat yearToSecTimeFmt = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
Date theDate = yearToSecFmt.parse("20230130120346");
System.out.println(yearToSecTimeFmt.format(theDate));
it will work as expected.
@Aiming Chen Thanks for the tip - I will definitely keep it in mind for future reference! Do you know if there will be fix to TalendDate.dateFormat?
In fact, the real issue come from the routine method TalendDate.TO_DATE(myDate,"YYYYMMddHHmmss")
if you use TalendDate.parseDate("yyyyMMddHHmmss","20230130120346") in your job , it will work as expected.
anyway , A jira is created for the issue, https://jira.talendforge.org/browse/TDI-49330