File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Problem inserting a date into postgres Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem inserting a date into postgres" Watch "Problem inserting a date into postgres" New topic
Author

Problem inserting a date into postgres

Lucas Sanabio
Greenhorn

Joined: Feb 19, 2005
Posts: 11
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<br />-----------------------------------------------------------------------------<br />Sun Certified Programmer for the Java 2 Platform<br />Consultor Java - Marx Tecnologia<br />Email: lucas@marx.com.br<br />URL: <a href="http://www.marx.com.br" target="_blank" rel="nofollow">http://www.marx.com.br</a><br />Cel: (62) 8117-5266
Lucas Sanabio
Greenhorn

Joined: Feb 19, 2005
Posts: 11
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

Joined: Sep 16, 2005
Posts: 1780
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:


There is no emoticon for what I am feeling!
Lucas Sanabio
Greenhorn

Joined: Feb 19, 2005
Posts: 11
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

Joined: Sep 16, 2005
Posts: 1780
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

Joined: Feb 19, 2005
Posts: 11
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

Joined: Sep 16, 2005
Posts: 1780
I don't know posgres, but there must be some configuration settings for
time zone, and that's what is causing the problem. Anyone?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem inserting a date into postgres