0

I am developing a Spring Boot app and there are some date/time fields in some entities. When considering an application will be used by multiple users from all around the world, the date and time fields should be treated properly and I am trying to follow one of the proper approach.

In this scene, what would you suggest to save date and time by users from different time zones. I think the following approaches:

  • I can save date and time based on UTC+0 and then when someone else retrieve data, I can show them based on their current time zone.

  • or save date and time based on the local time of the user with that local time zone.

I am really confused with this issue as I have no previous experience. And as far as I see, I can use LocalDateTime of java.time with Java 11+. But if you have another suggestion that is better than java.time, you can share also.

Jack
  • 1
  • 21
  • 118
  • 236
  • The former approach is less problematic. Consider that a user has their machine's time settings wrong. In the latter approach, that would be permanently reflected in what has been stored. In the former case, you might initially get bad results, but it would be open to correction by correcting the system time settings. It's not quite as simple as that, as it could be the case that the *real time clock* is misconfigured, in which case the value for UTC might be wrong – g00se Feb 18 '23 at 11:43
  • It's possible to get correct UTC from the Net too as a safeguard – g00se Feb 18 '23 at 11:49
  • @g00se So, could you post an example with the proper usage? – Jack Feb 18 '23 at 12:06
  • 1
    Take a look at the answers here: [What's the difference between Instant and LocalDateTime?](https://stackoverflow.com/q/32437550/12567365) The answers include overviews of the concepts, Java code examples, and guidance on usage - for example: "_Nearly all of your backend, database, business logic, data persistence, data exchange should all be in UTC. But for presentation to users you need to adjust into a time zone expected by the user. This is the purpose of `ZonedDateTime`..._" – andrewJames Feb 18 '23 at 13:24
  • @andrewJames This is the first useful answer I have seen today. Thanks a lot. – Jack Feb 18 '23 at 15:22

2 Answers2

1

I highly recommend using the ZonedDateTime object which holds information about the date, the time, and the regarding timezone. With the Java Date and Time API introduced with Java 8 it is very easy to convert dates, times, zones, and to calculate with them. To store these objects directly, refer to your database abstraction layer you are using. If needed, you can use the DateTimeFormatter to create String representations easily to view and store.

If you need a more detailed answer, please provide a more detailed question, preferably with example code.

McPringle
  • 1,939
  • 2
  • 16
  • 19
  • Thanks, but I am expecting example code from you as you try to explain some proper way. Any example pls? – Jack Feb 18 '23 at 12:07
  • Sorry, @rosy, but you should adjust your expectations. This is not a paid service. People answering here do this in their spare time. Should they write an application for you based on your really vague question? If you want this, you can check other websites to hire someone. Or be specific about your problem and show example code from **you** where you have a problem. – McPringle Feb 19 '23 at 13:18
  • Yes, unfortunately there are also some unnecessary comments by some users. – Jack Feb 19 '23 at 17:57
0

I will go for the first solution and give the client the time based on his/her zone. Keep in mind that the local time zone that you will display will be the time zone of the server which the client has called. To get the real time zone of the client you need to make sure to get it from the client itself. May be this link could help

  • I am confused after reading the answers that are completely suggest different things. Any example pls? – Jack Feb 18 '23 at 12:11