5

It is often recommended that datetimes be stored in the database in UTC and displayed to/received from users in their local timezone.

I am trying to implement this pattern in a Spring MVC / Spring Boot application but haven't been able to find documentation or examples. I'm using @DateTimeFormat annotations on the java.util.Date attributes of my @ModelAttribute form objects to make Spring parses/format dates.

Does Spring have built in support for this pattern? Something along the lines of Django's timezone-aware datetimes would be good. If not is there a not-too-cumbersome way of implementing it? Or is there a better way to handle/store datetimes in a Spring MVC application?

At the moment the application is UK-only so at present I only need support for BST (daylight savings), not different time zones for different users. However supporting users in different time zones is a potential future requirement.

Community
  • 1
  • 1
frankoid
  • 101
  • 2
  • 7
  • are you storing your date in a Date o Calendar object ? – reos Nov 16 '15 at 13:47
  • Did you ever figure this out? I am also very interested in this. – Andy Dec 09 '15 at 00:42
  • @reos, we're storing it as a java.util.Date at the moment – frankoid Dec 09 '15 at 17:48
  • @Andy not really. I discovered that in this environment dates and times are interpreted as being in the server JVM's time zone, so for the time being we're setting the server's time zone to the time zone our users are in (Europe/London) and we are using the 'timestamp with timezone' data type for date fields in our PostgreSQL database so that date calculations that span summer time changes work. We'll have to revisit the way we do things if & when the app needs to support user in other time zones. – frankoid Dec 09 '15 at 17:52
  • Okay, thanks for the feedback! – Andy Dec 09 '15 at 17:57
  • The Date object in java doesn't support timezone, you need to use Calendar instead. – reos Dec 09 '15 at 18:46

1 Answers1

0

Hope this will help you or at least will be the point from where you may start.

Looks like the best way is to use OffsetDateTime type for storing dates. It is easy to convert dateTime values coming from user to UTC:

OffsetDateTime createdOn = requestDto.getCreatedOn();
OffsetDateTime utc = createdOn.atZoneSameInstant(ZoneId.of("UTC"));

and back again (of course you need to store users time zone too):

OffestDateTime eventDate = modelObject.getEventDate();
OffsetDateTime userTime = eventDate.atZoneSameInstant(ZoneId.of(userTimeZone));

To instantiate new date object in UTC you may use clock-object:

OffestDateTime now = OffestDateTime.now(Clock.systemUTC());
ModelObject dto = new ModelObject();
dto.setEventDate(now);

And finally If you no need to show offset, you may use LocalDateTime type:

OffestDateTime eventDate = databaseModelObject.getEventDate();
LocalDateTime userTime = eventDate.atZoneSameInstant(ZoneId.of(userTimeZone)).toLocalDateTime());
ResponseDto response = new ResponseDto();
response.setEventDate(userTime);
Konstantin Konyshev
  • 1,026
  • 9
  • 18