0

Is there a way to convert a java.util.date in the format of:

Sat Jan 01 02:00:00 EET 2022

To datetime format in python?

I am importing a parameter from spss modeler into python that is read in java.util.date format and i wish to convert it in a format that is recognized by python.

thank you!

KRStam
  • 393
  • 5
  • 18
  • Please, check [ask]. Post [mre] of your code so far, incl. sample input. – buran Jul 01 '22 at 08:52
  • You will need to install [`pytz`](https://pypi.org/project/pytz/) to parse timezone by name. Let's say that your date string stored in variable `date_str`. You need to "extract" timezone name from string, simplest will be to [split](https://docs.python.org/3/library/stdtypes.html#str.rsplit) this string and assign parts to multiple variables: `date_part1, tz, date_part2 = date_str.rsplit(maxsplit=2)`. – Olvin Roght Jul 01 '22 at 09:18
  • Then you can parse concatenated `date_part1` and `date_part2` using [`datetime.strptime()`](https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime): `dt = datetime.strptime(date_part1 + date_part2, "%a %b %d %H:%M:%S%Y")`. And finally you will need to initialize `pytz.timezone()` using parsed timezone name *(stored in variable `tz`)* and call `localize()` on created [`datetime`](https://docs.python.org/3/library/datetime.html#datetime.datetime) object *(stored in variable `dt`)*: `dt = timezone(tz).localize(dt)`. – Olvin Roght Jul 01 '22 at 09:18

0 Answers0