Skip to content

Instantly share code, notes, and snippets.

@Wicowyn
Created December 23, 2017 21:18
Show Gist options
  • Save Wicowyn/9198567c2de865107bd4f75f9c6aa455 to your computer and use it in GitHub Desktop.
Save Wicowyn/9198567c2de865107bd4f75f9c6aa455 to your computer and use it in GitHub Desktop.
JPA Joda DateTime converter
public class DateTimeConverter implements AttributeConverter<DateTime, Date> {
@Override
public Date convertToDatabaseColumn(DateTime attribute) {
return attribute == null ? null: new Date(attribute
.withZone(DateTimeZone.UTC)
.withZoneRetainFields(DateTimeZone.getDefault())
.getMillis());
}
@Override
public DateTime convertToEntityAttribute(Date dbData) {
return dbData == null ? null
: new DateTime(dbData.getTime())
.withZoneRetainFields(DateTimeZone.UTC)
.withZone(DateTimeZone.getDefault());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment