Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem inserting a date into postgres

 
Lucas Sanabio
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, I'm having a weird problem inserting data into a date field into postgres. I developed the application on my own computer and it is running perfectly.

I got an date typed by the user, when I insert the date into the database the date is one hour earlier from the date the user typed. The first thing I looked is the system date and all summer time configuration, because the application was deployied on server which has the summer time configured. But I made some debugs and the date was correct, so I decided to log it just one line before inserting the it and I got the right date, but when a look at the database it is wrong.

The log I made:

2005-12-05 16:18:26,254 -> DEBUG (AudienciaAction.java:76) - Data criada: Tue Dec 06 20:00:00 BRST 2005
2005-12-05 16:18:26,265 -> DEBUG (AudienciaDAO.java:77) - Data na DAO:Tue Dec 06 20:00:00 BRST 2005
2005-12-05 16:18:26,265 -> DEBUG (AudienciaDAO.java:79) - Timestamp: 2005-12-06 20:00:00.0

The two last lines was printed one line before executed the sql, and the date displaied was the date I typed. This is the way insert de date:

***********
log.debug("Data na DAO:" + vo.getData());
Timestamp tt = new Timestamp(vo.getData().getTime());
log.debug("Timestamp: " + tt);
stp.setTimestamp(i++, new Timestamp(vo.getData().getTime()));
**********

The first two lines was just for debugging. The vo is an value object and the attribute data is an java.util.Date.

So, anyone has any idea why it is happening?

Thanks,

Lucas Sanabio.
 
Lucas Sanabio
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to say, the field type in the database is timestamp and the date inserted was: 2005-12-06 19:00:00
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what time zone you are in, but your time zone is used in
java.util.Date's toString method.

The idea is that java.util.Date and its subclasses java.sql.Date
and java.sql.Timestamp are essentially long values, representing the
number of milliseconds since the start of 1970 (UTC). Date doesn't
hold any timezone information
, so you should think of it as always
being in timezone UTC (no daylight savings time nonsense). If you
want to display a date/timestamp with respect to a certain time zone,
you have to use a DateFormat and configure it:
 
Lucas Sanabio
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I said that I looked for the summer time configuration because when I convert the date typed by the user to a java.util.Date I used the SimpleDateFormat convert.

try {
SimpleDateFormat sd = new SimpleDateFormat("dd/MM/yyyy HH:mm");
Date data = sd.parse(form.getData() + " " + form.getHorario());
vo.setData(data);
} catch (Exception e){
log.error("Erro ao criar a data do compromisso.");
log.error("Data: " + form.getData() + " " + form.getHorario());
}

But, anyway, it still saving the wrong date, do you have any idea why?

Thanks,

Lucas Sanabio.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's hard to tell, based on what you write.
If you take a date, insert it into the database, then retrieve it,
is it equal to the original date?
 
Lucas Sanabio
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, when I retrieve the date it is one hour earlier. It seems like the postgres is doing something with the date, but I didn't find anything about that, everywhere I looked they say the postgress get the date from the system, but it would be a problem if I tell the database to insert the current date, but I'm telling him which date I wanted. :-(
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know posgres, but there must be some configuration settings for
time zone, and that's what is causing the problem. Anyone?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic