• Post Reply Bookmark Topic Watch Topic
  • New Topic

Primefaces Calendar showing previous date than one in the database  RSS feed

 
Ranch Hand
Posts: 94
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's something fun which was partially answered in the Primefaces forum but doesn't help me fix it.

In my JSF view, I use Calendar to select a date and persist it to the DB, let's say 31/7/2017. It's saved in the DB as "2017-07-31T00:00:00Z". When I retrieve the date to show on the datatable, it shows the day before 30/7/2017. This happens for all dates, maybe the conversion fails somehow? No matter what date I have in the DB, the date shown in the datatable is the previous day.

This is what I do:

Someone said it's probably a timezone issue and asked where is the client and server located. I'm developing on my machine and haven't deployed online yet. I really don't know how to proceed, how can I check the timezones?
 
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Vasilis!

Timezones are part of the Java locale subsystem. I believe that locales are pre-configured with their defaults set to the part of the world you're in when you install the JDK/JVM, but I'm not sure. If not, it's probably looking at the timezone settings on your local machine.

I'm fairly certain that if your webapp doesn't explicitly override it, that the calendar control formats dates/times based on the timezone of the server - it does not determine from the zone of the client unless you write your own code for that.

For more information on locales, ask in our General Java forum.
 
Vasilis Souvatzis
Ranch Hand
Posts: 94
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've done some more digging, and it was indeed a time zone issue. The answer is here (why I didn't find it before asking here I don't know). Those folks had the exact same issue with me.

Apparently, the JSF date conversion defaults to UTC. The date type I'm using in Postgresql is its own, perhaps some timezone in the US because otherwise it'd have worked. So I guess the database type takes precedence and is minus one day in the app. Changing the deployment descriptor sets the JSF's converters to my system's timezone.

And with all that, it's evident that my post's title is wrong and will not show up in people's searches. Could it be changed to "JSF convertDateTime shows different date than database" or similar?
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't worry about the title.

There are several different datetime types in PostgreSQL as well as other databases. The one you'd prefer, I think is "timestamp WITH time zone".

This is what the PostgreSQL docs say:
postgresql manual wrote:
The SQL standard requires that writing just timestamp be equivalent to timestamp without time zone, and PostgreSQL honors that behavior.


So if you store "current date/time" in such an object, the local timezone will be used, but if you fetch it into a java.util.Date (or java.sql.Date), the constructor is going to assume that the timezone was UTC.
 
Vasilis Souvatzis
Ranch Hand
Posts: 94
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I don't want time of day or time zones though? I understand I should use timestamp with timezone to save timezone as well, I chose date as data type because I wanted to store just the date 01-08-2017.

I have no use of timestamps with or without time zones in my app. That's what confused me, I inserted dates but it behaved like dates+timezones.
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot have a date without a time. At 3:00AM August 4 in London, it's still August 3 where I live. In fact, I used to live in a small town in Florida where my house was in one time zone and my school was 5 miles away in another timezone, both in Florida. Indiana was even worse, since parts of it didn't participate in Daylight Savings (Summer) time and thus, the time zone effectively switched right under your feet.

OK, you say - but I don't need to be that picky/precise. Perhaps not, but java.util.Date and java.sql.Date (and corresponding Calendar classes) are not really granular to days, they're granular to microseconds. So a java.util.Date of "July 4, 2017" is in reality holding the value of July 4 2016 00h 00m 00.000s UTC. The PostgreSQL timestamp without timezone is actually more suited for scheduling things like at "4 AM January 3, 2038" local time delete all files than as a record of an actual date in the Einstein space-time continuum.
 
Vasilis Souvatzis
Ranch Hand
Posts: 94
1
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're absolutely right, I kind of figured what you explained about dates and timezones after I posted. After all, this is why all enterprise systems are extra careful with handling date and time. I understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!