• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Date problem

 
Raveendran Hs
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • 0
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • 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:

 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic