I'm having issues parsing a string to a date format.
This is my string
7/13/2022 9:46 AM
As per the documentation, I'm trying PARSE_DATETIME("%x %R %p", datetime_closed)
or PARSE_DATETIME("%D %E4Y %R %p", "7/13/2022 9:46 AM")
I've tried other combinations but none of them works
You can use the format below:
PARSE_DATETIME('%m/%d/%Y %l:%M %p', "7/13/2022 9:46 AM")
The output:
Use PARSE_DATETIME("%m/%d/%Y %l:%M %p", datetime_closed) to get datetime or DATE(PARSE_DATETIME("%m/%d/%Y %l:%M %p", datetime_closed)) to get date
Related
I have a problem with DataWeave 2 transformation. I have:
var parseDate = (dateStr) -> dateStr as DateTime {format: "yyyy-MM-dd"}
But when I am running this code I get:
Caused by: org.mule.runtime.api.el.ExpressionExecutionException:
Cannot coerce String (2019-03-26) to DateTime, caused by: Text
'2019-03-26' could not be parsed: Unable to obtain ZonedDateTime from
TemporalAccessor: {},ISO resolved to 2019-03-26 of type
java.time.format.Parsed
I am using DateTime cause it is detected as such when creating metadata. But the class itself has LocalDate bookingDate; - the problem is that when I am trying to use LocalDate - I get an error:
Unable to resolve reference of: `LocalDate`.
What can I do with this problem? Can I parse it somehow correctly? Or what can I do with the LocalDate problem mentioned above?
As your input string has only the date part, you can use the following DataWeave expression:
var parseDate = (dateStr) -> dateStr as Date {format: "yyyy-MM-dd"}
Input String: 201801
Output String format: 01.2018
I tried using following but it's not working, I also looked up for string to date convesrion/coercion table in "Type Coercion Table" https://docs.mulesoft.com/mule-user-guide/v/3.8/dataweave-types#type-coercion-table. I did not find something to help me with this.
as :date {format: "yyyyww"} as :string {format: "ww.yyyy"}
Appreciate if anyone has any ideas to share.
If you don't mind a bit of string hackery you could just move the various parts of the input string around:
%dw 1.0
%output application/json
%var inputDate = "201801"
---
{
outputDate: inputDate[4..5] ++ "." ++ inputDate[0..3]
}
The output of that is
{
"outputDate": "01.2018"
}
The above isn't null safe though, if there is a chance that the input date string is empty, or if it is shorter than 6 characters you will get a runtime error. You could work around that by checking the date in the DW, something like
outputDate: (inputDate[4..5] ++ "." ++ inputDate[0..3]) when inputDate != null and ( sizeOf inputDate ) == 6 otherwise null
I tried expression like below in mule dataweave but I am getting error.
pdate : "2017-06-22T12:45:55" as:datetime{format : "yyyy-Mm-dd'T'hh:mm:ss"} as :string{format: "MM/DD/yyyy"}
Can you please help on this if I missed anything to write.
try this:
%dw 1.0
%output application/json
---
{
pdate: "2017-06-22T12:45:55" as :localdatetime { format: "yyyy-MM-dd'T'HH:mm:ss" } as :string { format: "MM/dd/yyyy" }
}
changes compared to your version:
replace Mm with MM in first format string, thx #Dai
replace hh with HH in first format string
replace DD with 'dd' in second format string
I'm trying to parse a date with milliseconds using NSDateFormatter, and I'm getting weird results when I print out its time interval since 1970. Here's my test code:
var timeStr = "2015-09-29 14:32:42.297-07"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSx"
let date = dateFormatter.dateFromString(timeStr)!
let dateFormatted = String(format: "%.20f", date.timeIntervalSince1970)
print("date: \(dateFormatted)")
Result: "date: 1443562362.29699993133544921875\n"
Instead of ending with 297, it ends with something close to it. Why?
Seems this is just how doubles work. It's not related to NSDateFormatter. If I make a double with value 2.3 then print it to 20 decimal places, I get 2.99999990453636... or something.
i have a remote sql satabase, and i connect him by php with JSON.
I can get string and int data OK, but now i have a problem.... i have a datetime field on my SQL database, and i dont know how to get it with JSON into java.util.Date
this is the code i tryed, but it fails getting the Date correctly, it gives me an exception (java.lang.ClassCastException: java.lang.String)
code:
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
positions.add(new Position(json_data.getString("latitude"), json_data.getString("longitude"), (Date)json_data.get("timestamp")));
}
You will have to fetch the string sent in the JSON data and parse that string with something like SimpleDateFormat.
In my projects I have used two different solutions.
Storing the Date not as Date but as long(milisecs since 1.1.1980). In Java you can get this with date.getTime(). There should also a methode in PHP. The get the Date Object in Java just pass the long value to the constructor(date= new Date(long_value)).
With this Method you may have problems when dates comes from different time zones.
Write the Date as a formatted Date String in the JSON. How you encode the Date is up to you. A short sample give below. see[1] for further infos.
To get the Date you need a SimpleDateFormater.
DateFormat df = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
try
{
Date today = df.parse("2001.07.04 AD at 12:08:56 PDT");
System.out.println("Today = " + df.format(today));
}
catch (ParseException e)
{
e.printStackTrace();
}
[1]http://download.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html