JadiraOrg / jadira

Jadira Framework
Apache License 2.0
74 stars 44 forks source link

StringColumnDateTimeMapper throws an Exception when Joda Datetime contains zone offset in hours, not Zone ID #88

Open andrewko opened 4 years ago

andrewko commented 4 years ago

Hi,

I'm trying to use "org.jadira.usertype.dateandtime.joda.PersistentDateTimeAsUtcString" for saving Joda DateTime with a zone offset into a Postgre DB.

Serialization works, but deserialization throws an Exception:

java.lang.IllegalArgumentException: Invalid format: "2020-03-10T14:04:32.040_+05:00" is malformed at "+05:00"

The exception occurs only when the Joda Datetime is created with a zone with hours offset:

StringColumnDateTimeMapper mapper = new StringColumnDateTimeMapper(); DateTime nowWithOffset = DateTime.parse("2019-08-12T11:15:00+03:00"); String nowWithOffsetAsString = mapper.toNonNullValue(nowWithOffset); mapper.fromNonNullValue(nowWithOffsetAsString);

When the Joda Datetime is created with a zone with a ZoneID, everything works fine:

StringColumnDateTimeMapper mapper = new StringColumnDateTimeMapper(); DateTime nowWithOffset = DateTime.now().withZone(DateTimeZone.forID("Europe/Moscow")); String nowWithOffsetAsString = mapper.toNonNullValue(nowWithOffset); mapper.fromNonNullValue(nowWithOffsetAsString);

A possible sollution could be creating the DateTimeFormatter with

appendTimeZoneOffset(null, true, 2, 2)

instead of

appendTimeZoneId()

We use org.jadira.usertype:usertype.core:6.0.1.GA