Hibernate 6 column defined as "timestamp with time zone" does not show UTC offset in ZonedDateTime

999 views Asked by At

After upgrading Hibernate from 5.2 to 6.2.0.CR4, on PostgreSQL v13, column data type="timestamp with time zone", ex value: "2023-04-13 04:42:16.992755-04", defined as java.time.ZonedDateTime, in UTC offset shows letter 'Z' instead of 'UTC'.

output example

@Getter
@Setter
@ToString

@Column(name="updated_at")
private ZonedDateTime updatedAt;

PostgreSQL column

Apr. 13, 2023 04:46p.m. Z
Apr. 13, 2023 04:42p.m. Z
Apr. 12, 2023 04:13p.m. Z
Apr. 12, 2023 04:13p.m. Z

<hibernate.core.version>6.2.0.CR4</hibernate.core.version>

application.properties (springboot v3.x)

spring.jpa.properties.hibernate.jdbc.time_zone=UTC
spring.jpa.properties.hibernate.timezone.default_storage=NORMALIZE_UTC
  • on Hibernate 5.2 it was 'UTC' instead of 'Z' for timezone.

appreciate the help, --vs

1

There are 1 answers

1
vit_sin On

The whole issue is boiling down to update usage of java.util.Date(JDK8) to java.time.ZonedDateTime(JDK17) while showing the output of ZonedDateTime with TimeZone in JSF view(using PrimeFaces) under SpringFramework(w/ Spring Boot, and JoinFaces).

Possible solution found so far (if anyone found any better, please post it too):

<p:dataTable ...
<p:column>
    <h:panelGroup id="auditUpdatedAt">
        <h:outputText value="#{manageDummyView.myConvert(dummy.updatedAt)}">
            <f:convertDateTime type="zonedDateTime" pattern="MMM dd, yyyy hh:mma z"/>
        </h:outputText>
    </h:panelGroup
</p:column>
...
</p:dataTable>

dummy.updatedAt - ZonedDateTime object

Than later on in the Java back end code we add our conversion method

import java.time.ZonedDateTime;
import jakarta.faces.view.ViewScoped;
import org.springframework.stereotype.Component;

@Component
@ViewScoped
public class ManageDummyView implements Serializable {

    public ZonedDateTime myConvert(ZonedDateTime zdt)
    {
        if (zdt == null) {
            return null;
        }
        
        ZonedDateTime zdtZoned = zdt.toInstant().atZone(ZoneId.systemDefault());
        return zdtZoned;
    }

And thus I get correct zone value in GUI: "Jan. 19, 2024 09:00p.m. EST"

Thanks to @BalusC!