This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Java Date problem

 
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In java web application while storing the date into a database and rendering from the database do we need to convert the date in to UTC format

in DB i have the datefield datatype as datetime

The problem i am getting is the date stored in to the DB properly but while rendering its giving one day lesser than the current date if the server or db time zone is different.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a few things you have to understand about java.util.Date and java.sql.Date objects.

These objects represent an "absolute" moment in time, and they do not contain any timezone information by themselves.

When you want to display a date in a specific timezone, you can convert the Date object to a string for display by using a DateFormat object. You set the timezone on the DateFormat object, to let it know in what timezone you want to have the date displayed. For example:

 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hs Raveendran wrote:In java web application while storing the date into a database and rendering from the database do we need to convert the date in to UTC format....


No you don't (or you almost certainly don't). As Jesper said, Java dates (and most database dates too) are an offset from a fixed point in time; and that time is already UTC (or as close as makes no difference).

It's much more likely that your rendering issues have to do with choosing the wrong timezone for display. For example, the database more than likely displays time according to its local timezone by default (and Java definitely does). If you want to see the time in UTC form, then you should follow his advice.

Winston
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic