Skip to content

Instantly share code, notes, and snippets.

@arvindkgs
Last active October 8, 2021 14:01
Show Gist options
  • Save arvindkgs/98109be6ffdcc6ffa34ccb386aaf1051 to your computer and use it in GitHub Desktop.
Save arvindkgs/98109be6ffdcc6ffa34ccb386aaf1051 to your computer and use it in GitHub Desktop.
[Java Date Time] DateTime Handling

When saving dates in UTC, H2 will not save in UTC. To force setting all datetimes in UTC timezone in Junit add below to test case

@BeforeClass public static void setTimezone() { TimeZone.setDefault(TimeZone.getTimeZone("UTC")); }

Date - Time

To save all datetimes in UTC, add below spring.jpa.properties.hibernate.jdbc.time_zone= UTC

Datetime

  1. Use Instant, OffsetDateTime instead of java.sql.Timestamp in Entity class
  2. In spring, set following property to store datetime in UTC from Hibernate - spring.jpa.properties.hibernate.jdbc.time_zone=UTC OR use UTC specific SQL type in DB that will convert datetime from OS to UTC.
  3. If you are using Jackson (which is by default in spring) use object mapper with JavaTimeModule. Example:
        @Bean
        @Primary
        public ObjectMapper objectMapper() {
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper
                .registerModule(javaTimeModule)
                .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
                .enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
                .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
                .disable(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS);
            return objectMapper;
        }
  1. If you use Instant, then you will need custom serializer/deserializer to JavaTimeModule as follows and add to above object mapper:

            JavaTimeModule javaTimeModule = new JavaTimeModule();
            javaTimeModule.addSerializer(
                    Instant.class,
                    new JsonSerializer<Instant>() {
                        @Override
                        public void serialize(
                                Instant instant,
                                JsonGenerator jsonGenerator,
                                SerializerProvider serializerProvider)
                                throws IOException {
                            jsonGenerator.writeString(instant.toString());
                        }
                    });
            javaTimeModule.addDeserializer(
                    Instant.class,
                    new JsonDeserializer<Instant>() {
                        @Override
                        public Instant deserialize(
                                JsonParser jsonParser, DeserializationContext
     deserializationContext)
                                throws IOException, JsonProcessingException {
                            return Instant.parse(jsonParser.getText());
                        }
                    });
        

UTC

  1. To store date time in UTC use data type TIMESTAMP instead of DATETIME
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment