jhipster and ZonedDateTime

1k views Asked by At

when I persist a ZonedDateTime, the zone information is just discharged.

    ZonedDateTime now = ZonedDateTime.now(ZoneId.of("America/Santo_Domingo"));
    ZonedDateTime firstMomentInMonth = now.withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS);
    ZonedDateTime lastMomentInMonth = now.withDayOfMonth(now.toLocalDate().lengthOfMonth()).truncatedTo(ChronoUnit.DAYS).with(LocalTime.of(23, 59, 59, 999999999));

    log.debug("firstMomentInMonth =  {}", firstMomentInMonth);
    log.debug("lastMomentInMonth = {}", lastMomentInMonth);
    ledger = new Ledger();
    ledger.setStartDate(firstMomentInMonth);
    ledger.setEndDate(lastMomentInMonth);
    return ledgerRepository.save(ledger);

The output from the log:

  firstMomentInMonth =  2017-09-01T00:00-04:00[America/Santo_Domingo]
  lastMomentInMonth = 2017-09-30T23:59:59.999999999-04:00[America/Santo_Domingo]

And in the MySql Workbemnch :

 '6', '2017-09-01 00:00:00', '2017-09-30 23:59:59', 'OPEN', NULL, '1', '3'

, in the Workbench you can see that the zone information is just discharged. I found out when the app went to production on heroku, where the server is in another timezone than the users. Some finders come back with the wrong results.

Here from ledger.json:

"fields": [
    {
        "fieldName": "startDate",
        "fieldType": "ZonedDateTime"
    },
    {
        "fieldName": "endDate",
        "fieldType": "ZonedDateTime"
    },

Am I doing something wrong ?

1

There are 1 answers

0
Mavlarn On

Timestamp in MySQL has no zone info. So the zone info of your server on which you deploy the app is important. The time saved in db will be created to dateTime in java using server's zone info.

And for the date passing from client side to server, should also contain the zone info, like '+04:00'.

For heroku, you should find a way to set the zone of the java application. You can set it with system properties. Maybe heroku provides a way to set that.