I am trying to covert a date from one string format ("M/d/yyyy H:mm:ss") to another ("yyyy-MM-ddTHH:mm:ss") in Talend Open Studio. I've been reading through forum posts, blogs, and the help documentation, and all of the solutions I've found ultimately result in error. I think it's because most solutions are actually attempting to convert a date to a string or vice versa, but none of them are attempting, as I am, to convert a string to a date and then back again. (And I'm willing to be told that's the wrong way to approach this, but it seems like the natural way to do the conversion since, presumably, Talend knows how to convert all the parts of the Date type.)
My current iteration looks like this:
row2._ACTIVITY_DUE_DATE_==""?"":TalendDate.formatDate("yyyy-MM-dd HH:mm:ss", TalendDate.parseDate("M/d/yyyy H:mm:ss", row2._ACTIVITY_DUE_DATE_))
So if I feed in 5/18/2012 1:00:00 PM, I would want to get back 2012-05-18 13:00:00. I'm getting an unparsable date error, which I've included below. I don't understand why this is happening. The expression above should only be trying to parse the date if it's not equal to "", but apparently that's the value it can't parse:
Exception in component tMap_1
java.lang.RuntimeException: java.text.ParseException: Unparseable date: ""
at routines.TalendDate.parseDate(TalendDate.java:864)
at routines.TalendDate.parseDate(TalendDate.java:808)
at msm_extras.msm_activities_i360_tasks_0_1.MSM_Activities_i360_Tasks.tFileInputDelimited_1Process(MSM_Activities_i360_Tasks.java:2854)
at msm_extras.msm_activities_i360_tasks_0_1.MSM_Activities_i360_Tasks.runJobInTOS(MSM_Activities_i360_Tasks.java:3690)
at msm_extras.msm_activities_i360_tasks_0_1.MSM_Activities_i360_Tasks.main(MSM_Activities_i360_Tasks.java:3549)
Caused by: java.text.ParseException: Unparseable date: ""
at java.text.DateFormat.parse(Unknown Source)
at routines.TalendDate.parseDate(TalendDate.java:850)
... 4 more
When comparing strings in Java you should use .equals. So in your case you should use "".equals(row2._ACTIVITY_DUE_DATE) ? "" : ....
Everything else about your date format conversion looks fine.
Related
I am getting the strings like 1604341549 and want to convert them to normal date format like
12 feb 2012 4:00. Here is my implementation
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
try {
Date d = sdf.parse(date);
sdf.applyPattern("dd MMM yyyy hh:mm");
holder.v.setText(sdf.format(d));
} catch (ParseException ex) {
Log.e("Exception", ex.getLocalizedMessage());
}
I have checked the logs and it is showing the dates as String before the try block but it is not implementing in the try block instead it is giving an error " Unparseable date: "1604341549" ".
Looks like your date is a long that encodes timestamp in seconds so you don't need to parse a String, instead you should simply build a Date object using Date(long date) constructor. don't forget to convert seconds to milliseconds by multiplying it by 1000
java.time through desugaring
Consider using java.time, the modern Java date and time API, for your date and time work. Let’s first declare two formatters, one for your input and one for your desired output.
/** For parsing seconds since the epoch */
private static final DateTimeFormatter unixTimestampFormatter
= new DateTimeFormatterBuilder()
.appendValue(ChronoField.INSTANT_SECONDS)
.toFormatter();
/** For formatting into normal date and time */
private static final DateTimeFormatter normalFormatter
= DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
.withLocale(Locale.UK);
Now the conversion goes like this:
String inputStr = "1604341549";
Instant inst = unixTimestampFormatter.parse(inputStr, Instant.FROM);
String normalDateTimeStr = inst.atZone(ZoneId.systemDefault())
.format(normalFormatter);
System.out.println(normalDateTimeStr);
Output is:
02-Nov-2020 19:25:49
I am convinced that Shamm is correct in the other answer: your input strings contain so-called Unix timestamps, that is, counts of seconds since the Unix epoch on Jan 1, 1970 at 00:00 UTC. With java.time we can build a formatter that parses such.
For giving “normal” output I am using Java’s built-in date and time format. In this case for UK locale, but you should use your users’ locale for most content users.
What went wrong in your code?
First, you were trying to parse the input string as containing year, month, date, hour, etc., which it didn’t. So you were lucky to get an exception so you got aware that it’s wrong (SimpleDateFormat very often does not give that, leaving you believing that everything is fine when it isn’t).
SimpleDateFormat parsed 1604 as year, 34 as month, 15 as day of month and 49 as hour of day. It then objected because there weren’t any digits left for the minutes (and seconds).
Question: Doesn’t java.time require Android API level 26?
java.time works nicely on both older and newer Android devices. It just requires at least Java 6.
In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
In non-Android Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
On older Android either use desugaring or the Android edition of ThreeTen Backport. It’s called ThreeTenABP. In the latter case make sure you import the date and time classes from org.threeten.bp with subpackages.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Unix time
Java Specification Request (JSR) 310, where java.time was first described.
ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
Java 8+ APIs available through desugaring
ThreeTenABP, Android edition of ThreeTen Backport
Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
I'm trying to set the last modified filter in a azure data factory dataset dynamically.
I'm using the following expression:
#formatDateTime(adddays(utcnow(),-2),'yyyy-mm-ddThh:mm:ss.fffZ')
I'm getting the following error:
Activity Copy1 failed: Failure happened on 'Source' side. ErrorCode=UserErrorInvalidValueInPayload,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=Failed to convert the value in 'modifiedDatetimeStart' property to 'System.Nullable`1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' type. Please make sure the payload structure and value are correct.,Source=Microsoft.DataTransfer.DataContracts,''Type=System.Reflection.TargetInvocationException,Message=Exception has been thrown by the target of an invocation.,Source=mscorlib,''Type=System.FormatException,Message=The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.,Source=mscorlib,'
I'm also not able to preview the data with this filter. I guess something is wrong here. Any ideas?
From the error message I understand that the string represenation of the date is not supported by the calander.
The DateTime represented by the string is not supported in calendar
Why do you need to format the string for the comparison?
Actually the following commands are tested & working after publish & trigger:
#utcnow()
#adddays(utcnow(),-2)
It's the preview functionality in the front end that is not able to deal with the expressions. This will hopefully be solved by Microsoft.
Perhaps, as a workaround, you could use this expression to get a rid of the extra characters in your datetime expression:
#substring(formatDateTime(adddays(utcnow(),-2), 'o'), 0, 23)
I tested this with utcnow() and it should return the datetime in the desired format:
"value": "2019-04-12T10:11:51.108Z"
turns out you can solve the above prepending a conversion to string to your line above, so from
#formatDateTime(adddays(utcnow(),-2),'yyyy-mm-ddThh:mm:ss.fffZ')
change it to
#string(formatDateTime(adddays(utcnow(),-2),'yyyy-mm-ddThh:mm:ss.fffZ'))
it works on my end
Encountered the same problem in data flow:
currentUTC()
did not work for pulling the last modified file in blob storage but
currentTimestamp()
did
When I invoke the following code
moment('2020-01-01T00:00:00Z').endOf('month').utc().format()
I get the result
'2020-01-01T07:59:59Z'
when I would have expected to see
'2020-01-31T23:59:59Z'
Is this a bug or am I not using the API correctly?
I think the problem is you used endOf before you convert the Date in UTC.
You pass this Date : 2020-01-01T00:00:00Z but the browser understand it with your timezone so the "real date" is 2019-12-31T15:00:00Z.
So you must convert it to UTC first and then proceed your change/call/etc.
So, I tried that and it worked ! Tell me if the problem persist.
moment('2020-01-01T00:00:00Z').utc().endOf('month').format()
When date was 2018-03-21 19:40, i tried following code
var date = new Date();
console.log(date);
Output :
2018-03-21T16:40:53.755Z
Server is missing for 3 hours as you see. I fixed it by adding 3 hours but I think it's not a good way. How can i fix this problem with better way ?
I don't think the date is incorrect, if you look closely at the format it is being printed, it has a Z at the end, which means:
A suffix which, when applied to a time, denotes a UTC offset of 00:00;
often spoken "Zulu" from the ICAO phonetic alphabet representation of
the letter "Z".
I guess you are in a place separated by 3 hours from UTC.
Node.js uses this format to print Date objects by default, but you can print your local time using toLocaleString():
console.log(date.toLocaleString());
Your server is most likely in another time zone.
We're getting an "unable to bind to request" when calling a service with the following querystring:
/SomeService?playerid=59326&fromdate=4-1-2014&todate=12-11-2014
We have been using this querysting format for awhile now.
The problem is some either a change in 4.0.34, or something in the OrmLightCacheClient, which we had turned off for awhile and only just recently turned it back on.
If I change the dates to following format, it seems to work.
/SomeService?playerid=59326&fromdate=2014-4-1&todate=2014-12-31.
We can roll with the changed querystring date format for now, but wanted to report the error.
When supplying a date only (i.e. doesn't include a time) it should be unambiguously defined using the YYYY-MM-DD format.