Talend date and time combine - date

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");

Related

Unsure of date format 1200819 but need to convert to 08-19-20

The column is a nvachar(20).
select [shipment_posted_date_arch]
FROM [RxIntegrity].[dbo].[DiscrepancyReport_Receipts]
When I pull this, the date is in this format of 1200819 for that column. I need to convert to normal date.
This seemed to work:
cast(convert(nvarchar(20), (19000000 + CONVERT(int, shipment_posted_date_arch))) as date)

Error java.time.format.DateTimeParseException: could not be parsed, unparsed text found at index 10

I´m trying to pase the next String using LocalDateTime, but I always get de unparsed text found error:
Error java.time.format.DateTimeParseException: Text '2016-08-18 14:27:15.103+02' could not be parsed, unparsed text found at index 10
Here is my String: convertDate: '2016-08-18 14:27:15.103+02'
And my code:
public static LocalDate conversorStringToLocalDateTime(String convertDate) throws ParseException {
LocalDate dateTime =LocalDate.parse(convertDate);
return dateTime;
}
I guess is not too complicated, buy I´m not able to see the error. Could the +02 in the String be the cause?
tl;dr
OffsetDateTime odt = OffsetDateTime.parse ( "2016-08-18 14:27:15.103+02" , DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" ) ) ;
Details
The Answer by greg-449 is correct about the problem (using a date-only object for a date-time value) but not the solution.
That Answer uses LocalDateTime which unnecessarily throws away valuable information about the offset-from-UTC. A LocalDateTime does not represent a specific moment on the timeline, only a vague idea about possible moments depending on adjusting into a particular time zone.
The +02 is an offset-from-UTC meaning “two hours ahead of UTC”. So in UTC the time-of-day for this simultaneous moment is 12 hours, 2 hours less than your 14 hours. This does represent a specific moment on the timeline. This offset is the valuable information you are throwing away with a LocalDateTime rather than an OffsetDateTime.
The format of your string is in SQL format, which is close to standard ISO 8601 format. Merely replace the SPACE in the middle with a T. The java.time classes use ISO 8601 formats by default, so no need to specify a formatting pattern.
String input = "2016-08-18 14:27:15.103+02";
String inputModified = input.replace ( " " , "T" );
Unfortunately, Java 8 has a bug in parsing offset values abbreviated to just an hour or offset values omitting the colon between hours and minutes. Fixed in Java 9. But in Java 8, we need to adjust the input.
// Workaround for Java 8 where 2-digit offset fails parsing. Fixed in Java 9.
int lengthOfAbbreviatedOffset = 3;
if ( inputModified.indexOf ( "+" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
if ( inputModified.indexOf ( "-" ) == ( inputModified.length () - lengthOfAbbreviatedOffset ) ) {
// If third character from end is a PLUS SIGN, append ':00'.
inputModified = inputModified + ":00";
}
Now parse.
OffsetDateTime odt = OffsetDateTime.parse ( inputModified );
Dump to console. Note how we transformed +02 into +02:00.
System.out.println ( "input: " + input + " | inputModified: " + inputModified + " | odt: " + odt );
input: 2016-08-18 14:27:15.103+02 | inputModified: 2016-08-18T14:27:15.103+02:00 | odt: 2016-08-18T14:27:15.103+02:00
Alternatively, specify a formatting pattern. The offset-parsing bug does not bite when using this formatting pattern.
DateTimeFormatter f = DateTimeFormatter.ofPattern ( "yyyy-MM-dd HH:mm:ss.SSSX" );
OffsetDateTime odt = OffsetDateTime.parse ( input , f );
Database
Coming from Postgres, you should be retrieving the value as a date-time object rather than a String.
If your JDBC driver complies with JDBC 4.2 you can call ResultSet::getObject to get an Instant or OffsetDateTime. If not, call ResultSet::getTimestamp to get a java.sql.Timestamp, then immediately convert to java.time by calling toInstant on the Timestamp object.
Stick with java.time for your business logic; use the java.sql types briefly and only for exchange with the database.
Your code is using LocalDate which only parses a date - not a date and time so you are getting an error when the parse finds the space after the date.
So you should be using LocalDateTime but LocalDateTime.parse(String) expects an ISO format date which is not the format you are using.
So you need to use a DateTimeFormatter to specify the format of your input string. Something like:
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSX");
LocalDateTime result = LocalDateTime.parse(convertDate, format);

Groovy - Converting a date string to a formatted date

OK, I am trying to convert a date string from a format like:
2014-01-21 00:00:00
to
01/21/2014
I have tried many variations and am crashing and burning. The issue is that to test I have to create the script, export it in a process in Bonita (a BPM software), Import it and then create some cases. This all takes a long time.
Hopefully someone knows how to do this.
Also, is there a simple groovy editor out there? That would help me learn how to write groovy very quickly.
Groovy Dates have methods parse and format for converting to and from strings in various formats:
def format1 = '2014-01-21 00:00:00'
def format2 = Date.parse("yyyy-MM-dd hh:mm:ss", format1).format("dd/MM/yyyy")
assert format2 == '01/21/2014'
The format of the format strings are the same as Java's SimpleDateFormat.
String olddate='2014/01/21 00:00:00'
Date date = Date.parse("yyyy/MM/dd HH:mm:ss",olddate)
String newDate = date.format( 'MM/dd/yyyy' )
log.info newDate

How to write expression to convert yyyymm to mm-yyyy in ssrs?

I have a table with a YearMonth column (201302) in it.
I created YearMonth a parameter.
Now I would like to write an expression in SSRS so that every time the report runs the date and month would look like this Feb-2013.
Could you please suggest me why following expression did not work.
Thanks
=CStr(Right(MonthName(Month(Parameters!NLR_YearMonth.Value)),3))
+ "-" +
Left(Year(Parameters!NLR_YearMonth.Value),4)
Try This:
=Format(DateValue(MonthName(Right(Parameters!NLR_YearMonth.Value, 2))
+ "," +
Left(Parameters!NLR_YearMonth.Value,4)), "Y")
The expression goes as follows:
Cutting the string to get the month.
Converting the mount to MountName
Creating a comma seperator between the month and the year
Cutting the string to get the year
Converting the returns string to Date object using the DateValue function
Formatting the Date to fits your needs
The results should be:
February, 2013

Parsing String to date doesnt work

I tried parsing a string in a namedQuery, but it seems doesnt work. I have this code in my domain class:
searchBirthdaten{ q ->
def dates = Date.parse("yyyyy:MM:dd HH:mm:ss", "2011-9-21 00:00:00")
eq 'birthDate' , dates)
}
But I always got this error:
Unparseable date: "2011-9-21 00:00:00"
I really dont understand why this is happening. Any idea?
Your date input string has to be in the format you defined: yyyy:MM:dd HH:mm:ss (corrected)
So your 3 issues were:
You are using the "-" character to delimit you date for parsing but your format string is using ":"
You have 5 ys in your format string i.e. yyyyy:MM.... Which won't be valid for another 8 thousandish years ;)
You define your month format as MM but you are passing only '9', this will need to be '09' to match your fomat string.