Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How to deal with users in different time zones? (mysql + hibernate)

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
I hope you all had a great holiday.

I have an app that save some dates. That's it. Very simple, I use hibernate and mysql.
Im using this on the database URL: "...&useTimezone=true&serverTimezone=UTC"

However, I have two issues:

1- When I save, for example, if I save the date: 19/01/2020, it saves as:18/01/2020 on the database.

2- How do deal when a user from UTC -2 saves a datetime and another user at UTC-3, for example, tries to read or change it?








 
Marshal
Posts: 24950
61
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the values you're saving are just dates, then time zones are irrelevant anyway. They only become relevant if you're trying to save date/time pairs, aka timestamps. So preferably you'd be using LocalDate objects, which represent a local date without time zone information, rather than some other type.

However you mentioned Hibernate in your post title, so perhaps you're using Hibernate as well? Seeing some code might help.
 
Saloon Keeper
Posts: 11188
244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you only have one server hosting your application, or if they all share the same time zone, the problem is not on the server side.

The problem is caused because clients send their local date in their local time zone. The client first needs to convert their date to the date that is applicable in an agreed time zone (either the server's time zone, or UTC). If that's too difficult to do on the client side, you may do the conversion on the server side, but the client then also needs to send its time zone to the server.

As Paul said though, we need some more specifics. What database framework (and framework version) are you using? What are you using the dates for? In what language are the clients written?
 
Saloon Keeper
Posts: 21603
147
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:If the values you're saving are just dates, then time zones are irrelevant anyway.



That's not true. There can be a period up to 5 hours long where the date where Campbell is located is different to the date where I am. Even more at Ranch HQ, and don't even think about my friends in Australia.

While other options are available, the best general option is to store the date in the database as a UTC value and translate it as needed based on the user's locale. Or, alternatively, simply warn them that it's a UTC date. In particular, if other time zones start chiming in later, it's future-proofed.
 
Castiel Snow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Thanks for all the replies.

Ok, Here go the specifics.


  • Java 11

  • Hibernate

  • Java 11


  • Im using LocalDateTime objects. Im not applying any kind of Zone logic. Just saving system default.



    Users connect to the database via their app and save information that contain datetime and sometimes just date.
    Users connect to the database via their app and load/read information from themselves and others. Those informations contain datetime and date and need to be converted appropriately to their zonetime.


    ----------

    I came up with my own solution.
    -----





    Using those methods, I came up with this logic:


    1-ToSave

    Create LocalDateTime
    Set Date Time with user input

    convert to UTC

    persist();



    2-Load

    getLocalDateTimeWithHibernate();

    Convert to system Default Zone

    show information to user;






    ------

    Problems/Questions:

    1- Brazil has removed summerTime/DayLightSaving? since last year but when I print my "printDateTime(getZoned(utc));" it comes as if it was still summertime. Why?

    2- Is there any way to automatize this in hibernate or I should do it manually? I dont care to do it manually, Im just curious if theres a better way.















     
    Tim Holloway
    Saloon Keeper
    Posts: 21603
    147
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Castiel Snow wrote:

    Problems/Questions:

    1- Brazil has removed summerTime/DayLightSaving? since last year but when I print my "printDateTime(getZoned(utc));" it comes as if it was still summertime. Why?

    2- Is there any way to automatize this in hibernate or I should do it manually? I dont care to do it manually, Im just curious if theres a better way.



    1. Probably your systems timezone database is out of date (no pun intended). I can't recall if Java has a self-contained timezone database, but I think that at least on Unix-like systems, it may use the system tzdata database. So you may need to update that. If Java's timezone data is self-contained, you'd have to update Java itself. Which is one reason why I think it's NOT self-contained, since I get tzdata updates on a suprisingly frequent basis - and it would reduce the write once/run anywhere abilities of Java if you had to have a specific JVM version just to have proper summer time detection.

    2. Yes, I suppose you could add a database trigger. But I don't recommend it. It's better to keep this function in the business logic and let all the lower levels deal in UTC dates.

















     
    Rancher
    Posts: 4450
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What is the app the users are using, and do you have control over that?
     
    Stephan van Hulst
    Saloon Keeper
    Posts: 11188
    244
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Castiel Snow wrote:

  • Hibernate

  • What version?

    Just saving system default.


    Don't do this. Either save an Instant or a ZonedDateTime, or save a LocalDateTime after adjusting to UTC from the server's actual time zone. Saving the server's LocalDateTime without adjusting to UTC WILL lead to problems eventually. I strongly recommend using Instant if you don't need to perform any day/month/year manipulations on the server because Instant gets rid of all the time zone pain.

    1- Brazil has removed summerTime/DayLightSaving? since last year but when I print my "printDateTime(getZoned(utc));" it comes as if it was still summertime. Why?


    Because you're using Java 11 and the time zone changes were introduced in Java 13. There might be an update for Java 11, I suggest you search for it.

    2- Is there any way to automatize this in hibernate or I should do it manually? I dont care to do it manually, Im just curious if theres a better way.


    For timestamps, just use Instant. Hibernate will map it to a timestamp with zone UTC. You will also want to sent Instant (or an equivalent date/time representation in UTC) to the client if they can manage their own conversion to the user's time zone.

    If the client can not handle their own date time conversion, they will have to send their time zone to the server upon get/set requests.

    If you want to send just a date from the client (and not a complete time stamp) you still need to send the time zone as well and you need to convert the date to an Instant on the server and save it as a timestamp regardless, because 2020-01-20 where the user is located really may be 2020-01-21 where the server is located, depending on what time it is.
     
    Paul Clapham
    Marshal
    Posts: 24950
    61
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Tim Holloway wrote:

    Paul Clapham wrote:If the values you're saving are just dates, then time zones are irrelevant anyway.



    That's not true. There can be a period up to 5 hours long where the date where Campbell is located is different to the date where I am. Even more at Ranch HQ, and don't even think about my friends in Australia.



    If you say "Store the current date into the database" then yes, what you said there is true. But if you say "Store the date 2019 October 17 into the database" then what I said is true -- it doesn't matter where you are or what the date is when you say that.

    So far we don't know what the OP wants to do and it seems like a lot of the responses are assuming that the current date is what's required.
     
    Tim Holloway
    Saloon Keeper
    Posts: 21603
    147
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    December 30 2007 (GMT) was still December 29 2007 at 7pm my local time and 4 pm Ranch time. "Current" doesn't mean anything. "Abstract", maybe. But dates do not exist in a vaccuum, and if it's important enough to worry about the time zone, it's worth worrying about what the date was in some particular zone.
     
    Castiel Snow
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for all the replies. Im working on the changes with those informations. I ran some tests and got out exactly what I wanted.

    Im just having one issue.
    Im running this on javafx and I have a table that contains a cell with a date, which used to be "LocalDate". However, since I changed the type of the variable to ZonedDateTime, I also changed it on the column, as follows:



    TO




    The problem is, the datepicker wont work anymore. Obviously.
    I want to use the datepicker here but I want the datepicker to return me a ZonedDateTime instead.
    The ZonedDateTime just has to return me the user input date, not time. But Since Im using "ZonedDateTime" on the TableColumn, the datepicker has to return me that kind of object, right?

    Im not having luck/understanding on how to make the datepicker give me a ZonedDateTime object.
    Please help.



    Error in line " this.datePicker.valueProperty().addListener(listener);"
     
    Dave Tolls
    Rancher
    Posts: 4450
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Can't you take the date as a LocalDate and convert to a ZonedDateTime?

    There are at methods to help with this (eg atStartOfDay).
     
    Castiel Snow
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:Can't you take the date as a LocalDate and convert to a ZonedDateTime?

    There are at methods to help with this (eg atStartOfDay).



    Yes, but im having trouble adapting the code.
     
    Dave Tolls
    Rancher
    Posts: 4450
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You revert to using LocalDate for the picker, then convert to ZonedDateTime when you create the persistence object.
     
    Castiel Snow
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Dave Tolls wrote:You revert to using LocalDate for the picker, then convert to ZonedDateTime when you create the persistence object.



    I tried but Im not getting how to deal with all these nested handlers? can you show me an example, please?
     
    Dave Tolls
    Rancher
    Posts: 4450
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, you had the picker working with LocalDate, so you'll need to show us what you have now, especially the conversion between LocalDate and ZonedDateTime.
     
    I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
    Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!