Convert String to date

Hello
So can you please help me? I have the date as a string in this formát :
Fri Nov 19 2021 18:02:03 GMT+0100 (Středoevropský standardní čas)
And I need date as the date.
I try to parse for the date but I have a problem with the format :frowning:
Thank you

I try this :
system.date.parse(timeEnd, “EEEE MMM dd yyyy hh:mm:ss Z zzzz”)
but it dosent work :frowning:

Try using strptime()
https://www.tutorialspoint.com/python/time_strptime.htm

Try this: EEE MMM dd yyyy hh:mm:ss zZ (zzzz)

Not working :frowning:
I tried :
system.date.parse(timeEnd, “E MMM dd yyyy HH:mm:ss zZ zzzz”)
system.date.parse(timeEnd, “E MMM dd yyyy HH:mm:ss zZ (zzzz)”)
system.date.parse(timeEnd, “EEE MMM dd yyyy HH:mm:ss zZ zzzz”)
But it doesn’t work :frowning:

When I tried this system.date.parse(“Fri Nov 05 2021 17:51:17”, “E MMM dd yyyy HH:mm:ss”) everything work good.
The problem will be in the zZ zzzz .
So any idea, please?

Sorry, I’m out of ideas. It does appear to be related to the Středoevropský standardní čas portion. Is your gateway and the server it’s on all set up for a locale/region where that would make sense?

I mean that yes I am from Czech Republic. And this App we use in or company with this setting.

And when i try this :
system.date.parse(“Fri Nov 05 2021 17:51:17 +0100 (Středoevropský standardní čas)”, “E MMM dd yyyy HH:mm:ss z”)
It works well, the problem seems to be in GTM

So i use this and seems to be it work :smiley: But i know that is not the best solution :frowning:
system.date.parse(timeStart.replace(“GMT”,""), “E MMM dd yyyy HH:mm:ss Z”);

I think that’ll have to do for now. I cannot for the life of me figure out how to convince Java to parse that String into a Date. It seems convinced it’s an invalid combination of things for some reason.

Thank you for helping and your time :smiley:

system.date.parse uses SimpleDateFormat under the hood. I went with java.time libraries and a regex to filter out stuff we don’t need. Hope this helps!

def toDate(stringIn):
	""" Specific function to convert string timestamp to local java.util.Date
	    Example: 
	            stringIn: 'Fri Nov 19 2021 18:02:03 GMT+0100 (Středoevropský standardní čas)'
	             returns: Fri Nov 19 12:02:03 EST 2021
	"""
	from java.time.format import DateTimeFormatter
	from java.time import ZonedDateTime
	from java.util import Date, Locale
	import re

		
	format = DateTimeFormatter.ofPattern('EEE MMM dd yyyy HH:mm:ss zxx').withLocale(Locale('en_US'))
	regex = r'(.*) \('
	
	#Get timestamp from the string
	tStampString = re.search(regex, stringIn).groups()[0]
	
	# Parse timestamp string to Instant
	zdt = ZonedDateTime.parse(tStampString, format).toInstant()
	
	#Convert to java.util.Date
	return Date.from(zdt)


dateString = 'Fri Nov 19 2021 18:02:03 GMT+0100 (Středoevropský standardní čas)'

print toDate(dateString)
1 Like

I tried your example in Script console, but this is what I get:

Hmm. Wonder if it’s a locale thing. I’ll look further when I get a chance.

I was able to replicate. It is a locale thing.
Updated the above script to use en_US as the locale of the formatter, then tried a few different locales. See if that does it.

1 Like

Yup... :+1: