my problem is that when I do a query on the database with a order by startTime where the startTime is a Joda time, it is returning the times starting at 4pm.
Now, 4pm is exactly 8 hours away from 12 midnight, and coincidentally I live in Hong Kong and that is my time zone and Hong Kong timezone is 8 hours from midnight.
My startTime JPA field is:
@Index(name = "bookingStartTimeIndex")
@Column(name = "start_time")
@Type(type="org.joda.time.contrib.hibernate.PersistentLocalTimeAsTime")
private LocalTime startTime;
I use JPA/Hibernate and my query is basically to select * where:
order by b.startTime asc
Now I have entities with times from 9am to 6pm stored in this table. What comes back from this “order by” is entities starting with time 4pm going to 6pm then at 9am to 4pm.
So it looks like it has decided that 4pm is midnight somehow.
This is a postgresql database, when I look at my database colum has a datatype of time_without_time_zone.
So how can I get it to order the entities normally from 9am to 6pm?
A workaround I found is to change to use PersistentLocalTimeExact, this works for
me under the conditions I have mentioned and solves all the problems I had with 4pm…
@Index(name = “bookingStartTimeNumberIndex”)
@Column(name = “start_time_number”, nullable = true)
@Type(type=”org.joda.time.contrib.hibernate.PersistentLocalTimeExact”)
private LocalTime startTimeNumber;
This sounds like an issue of data presentation rather than a fundamental problem, though it is a little unclear.
Are you saying that for a record in the database that has a time of 0:00 hours it displays as 16:00 hours when you get it back from the database? If so, then I think there’s some confusion over reading the date from the database and somewhere in Hibernate extension for Joda Time is creating the
LocalTimeinstance assuming the database time is UTC and then creating an instance ofLocalTimewith your local timezone, thus applying a -8 hour offset.As Erwin says, storing a timezone in the database is the better way to go, or if you can’t do that then only store UTC dates and/or times.
I had a look at the Joda Time website and they are recommending a different Hibernate extension as better supported than the original one you are using.
You can also try breakpointing the
PersistentLocalTimeAsTimeclass and see how theLocalTimeis being constructed.