Dataweave: Cannot coerce String to Datetime - mule

So I need to transform this String : "2023-01-30T15:00:00+0100" to Datetime in order to send to SF.
I've tried like this :
StartDateTime: payload.planDate as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"} and instead of "DateTime" , "LocalDateTime".
None of them seem to work.
Both give me this error :
Cannot coerce String (2023-01-30T15:00:00+0100) to DateTime, caused by: Text '2023-01-30T15:00:00+0100'
Could you help me transform the String to DateTime?
Expected Result : "2023-01-30T15:00:00+0100"

The input data is a DateTime because it includes a timezone. Your format is incorrect because it has milliseconds and the input has not. Removing that should be OK.
Example:
%dw 2.0
output application/json
var planDate="2023-01-30T15:00:00+0100"
---
planDate as DateTime {format: "yyyy-MM-dd'T'HH:mm:ssZ"}
Output:
"2023-01-30T15:00:00+0100"
Note that the output is a string because the output format is JSON and JSON doesn't has a date type. If you output other in a different format you will see the DateTime output. If you intend to use the resulting DateTime for calculations in the flow then application/java is the best output format for performance.

Related

convert dateTime to salesforce required dateTime format in mule 4

For the dateTime "2022-11-29T19:12:21Z" getting INVALID_TYPE_ON_FIELD_IN_RECORD error from salesforce. So need to convert it in correct format that Salesforce accept.
Input: "2022-11-29T19:12:21Z"
Expected output: 2022-11-29T19:12:21.000+0000
I tried this script:
{
"lastDateTime": "2022-11-29T19:12:21Z" as DateTime {format: "yyyy-MM-dd'T'HH:mm:ss.SSS"}
}
Please help to get the expected output format.
A DateTime must have a timezone so you can not just parse the input string into it directly. The input also misses milliseconds so it can not be parsed with the milliseconds format .SSS.
You can convert to a LocalDateTime first, then add a timezone (+0:00), then format to string with milliseconds and timezone. A DateTime must have a timezone so you can not just parse the input
%dw 2.0
output application/json
---
{
"lastDateTime": ("2022-11-29T19:12:21Z" as LocalDateTime {format: "yyyy-MM-dd'T'HH:mm:ss'Z'"} ++ |+00:00|) as String {format: "yyyy-MM-dd'T'HH:mm:ss.SSSZ"}
}

Date to DateTime conversion in DataWeave 2

Input date is coming in this format "2022-04-30" and I want to convert it to date time and time should be zeros like 2022-05-15T00:00:00Z.
How it can be achieved using DataWeave 2.0?
Convert the input string to a Date, then add the time to create a DateTime output. Then just convert to String. The default pattern seem to be adequate that you need.
%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
(sourceDate as Date {format: "yyyy-MM-dd"} ++ |00:00:00Z|) as String
Output:
"2022-04-30T00:00:00Z"
If you want the result as String you can just Append "T00:00:00Z" to your date string
%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
sourceDate ++ "T00:00:00Z"
And if you want a DateTime Object you can just coerce it to DateTime using as keyword. It will work as your date string is already in the required format.
%dw 2.0
output application/json
var sourceDate="2022-04-30"
---
(sourceDate ++ "T00:00:00Z") as DateTime

How to convert string to date type in the Pentaho?

I am try to convert string value to date. The string has this format : yyyy-MM-dd. But when I try to convert using select values (in meta-date I selected fildname, type = Date and currency = dd/MM/yyyy I got this error :
String : couldn't convert string [2017-01-30] to a date using format [yyyy/MM/dd HH:mm:ss.SSS] on offset location 4
If I do in calculator step : Create a new field, Final_date as a Copy of field A; on Field A put the name of your input string; Data type is date and on Conversion mask choose the yyyy-MM-dd format (you don't have to pick one from the dropdown menu, you can write your own). I got the same error.
I am using the Pentaho Data Intagration 9.
I am try to convert the string format in yyyy-MM-dd to date type format in dd/MM/yyyy. For this case, how to convert string to date ?
When converting from string to date you specify the source format that the string is using, so in this case yyyy-MM-dd. That should be in the format selection list, but you can also manually type in any format needed.
Once the field is in date format, it will be correctly output to most database types. For files, you can define the new format (dd/MM/yyyy) in the output step like Text File Output or Excel Writer. Alternatively, you convert the date back into a string with the desired format using Select Values.

NULL values in a string to date conversion

I have a table with the following data:
logs.ip logs.fecha logs.metodo
66.249.93.79 19/Nov/2018:03:46:33 GET
All data columns are string and I want to convert logs.fecha into date with the following format: YYYY-MM-dd HH:mm:ss
I try the following query:
SELECT TO_DATE(from_unixtime(UNIX_TIMESTAMP(fecha, 'yyyy-MM-dd'))) FROM logs
Results of the query are NULL in all rows.
How can I make the conversion string to date for all rows? I know I must use ALTER TABLE but I don't know how to do it.
Thanks
The reason you get null is because the format of the input string is different from the input passed to unix_timestamp. The second argument to unix_timestamp should specify the string format of the first argument. In from_unixtime you can specify the output format desired. If nothing is specified, a valid input to from_unixtime returns an output in yyyy-MM-dd format.
The error can be fixed as below.
from_unixtime(unix_timestamp(fecha,'dd/MMM/yyyy:HH:mm:ss'),'yyyy-MM-dd HH:mm:ss')
You just have to tell Oracle the date format you are reading with TO_DATE.
Try:
SELECT TO_DATE(fecha,'DD/MON/YYYY:HH:MI:SS') FROM logs

Date format is not getting displayed

I have referred to solutions provided for date in yyyy-MM-dd format. After providing the format also, I am getting date in default format. Please help.
code:
Dim returndate As String
returndate = DateTime.Now.ToString("yyyy-MM-dd")
Dim oDate As DateTime = DateTime.ParseExact(returndate, "yyyy-MM-dd", CultureInfo.InvariantCulture)
As it stands I'm guessing your output of oDate is 27/01/2017 00:00:00.
What you want to do is this:
Dim returndate As String = DateTime.Now.ToString("dd/MM/yyyy")
Dim oDate As DateTime = DateTime.ParseExact(returndate, "dd/MM/yyyy", CultureInfo.InvariantCulture)
Debug.Print(oDate.ToString("yyyy-MM-dd"))
Send the returndate into DateTime.ParseExact with the format of "dd/MM/yyyy" and then format oDate to the desired output, in your case "yyyy-MM-dd".
This is a screenshot of the code and output:
Dates don't inherently have a format. The format you pass to ParseExact is the format it expects of the first parameter, in your case returndate. It's not the output format. Have a look over the DateTime.ParseExact documentation:
format
A format specifier that defines the required format of s. For more information, see the Remarks section.
Looking at the Remarks section:
The DateTime.ParseExact(String, String, IFormatProvider, DateTimeStyles) method parses the string representation of a date, which must be in a format defined by the format parameter. It also requires that the date and time elements in s appear in the order specified by format. If s does not match the pattern of the format parameter, with any variations defined by the style parameter, the method throws a FormatException.