• Post Reply Bookmark Topic Watch Topic
  • New Topic

timestamp  RSS feed

 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm making an insert into mySql (using php):
INSERT INTO posts (data, titulo, texto) VALUES ( $data, '$titulo', '$texto')
where:
$data = $_SERVER['REQUEST_TIME'];

now, i'm trying to read it using java:
out.println(new java.util.Date(resultSet.getInt(2)));

but i allways get:
Wed Jan 14 10:55:45 GMT 1970, or
Wed Jan 14 11:56:08 GMT 1970 and so on, but
these dates refer to this year

How can i manage this?

TiA
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

out.println(new java.util.Date(resultSet.getInt(2)));

Why are you using ResultSet's getInt() method, rather than its getDate() method? And how are you getting your code to compile when you use an integer in the Date's constructor?
[ November 17, 2006: Message edited by: Paul Sturrock ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using ResultSet's getInt() method, rather than its getDate() method?

ignorance
anyway i followed your suggestion but now i get:
out.println(resultSet.getDate(2));
1161-11-11, or
1158-11-02
And how are you getting your code to compile when you use an Integer in the Date's constructor?

it does compile, since i can read the servlet's output at my localhost...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
And how are you getting your code to compile when you use an integer in the Date's constructor?

That isn't a problem; class java.util.Date has a constructor that takes a long, and if you put in an int, it is automatically converted to a long.

But as Paul says, you should be using the getDate() method of the ResultSet. Note that this returns a java.sql.Date, but that isn't a problem, since that is a subclass of java.util.Date. So just do:

Note: If you want to format your date in a specific format, you'll need to use java.text.SimpleDateFormat for that.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
anyway i followed your suggestion but now i get:
out.println(resultSet.getDate(2));
1161-11-11, or
1158-11-02

What is the type of that column in the database? Is it a DATE or some other date or timestamp related type, or something else?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

That isn't a problem; class java.util.Date has a constructor that takes a long, and if you put in an int, it is automatically converted to a long.

D'oh! I briefly forgot ResultSet's getInt method returns an int not an Integer. Oh well...
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the type of that column in the database? Is it a DATE or some other date or timestamp related type, or something else?

in my MySql database the column type for that timestamp is integer
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by miguel lisboa:
in my MySql database the column type for that timestamp is integer

And how are dates stored in this column? What does the value in the database represent? A number of days, hours, minutes, seconds, milliseconds etc. since a fixed date?

java.util.Date counts milliseconds since January 1, 1970. Obviously your database column doesn't contain milliseconds since 1-1-1970, but some other representation. Since you're inserting the data with PHP, you'll have to look in the documentation of PHP to see how it represents dates. You have to find out what it is and convert the value accordingly.
[ November 17, 2006: Message edited by: Jesper Young ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how are dates stored in this column? What does the value in the database represent? A number of days, hours, minutes, seconds, milliseconds etc. since a fixed date?

(from php docs):
the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
java.util.Date counts milliseconds since January 1, 1970. Obviously your database column doesn't contain milliseconds since 1-1-1970, but some other representation.

i guess it does: 1149805105, or 1149851870 and so on
so, as both start dates are similar, i'm puzzled
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read carefully:

PHP: the number of seconds since 1-1-1970
Java: the number of milliseconds since 1-1-1970,

So, what should you do to convert seconds (what PHP puts in the database) to milliseconds (what Java expects)?

Note: resultSet.getDate(...) will not work if you have to convert the value first, but getInt(...) is also not good enough. You'll have to use getLong(...) and put the value in a long. The number of milliseconds since 1970 is larger than wat a 32-bit int can hold.
[ November 17, 2006: Message edited by: Jesper Young ]
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
milli!
but now i feel dumb, since i cant alter database values, and cant multiply a date
confess i have no idea
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, one idea was to get how many miliseconds are in php date (resultSet.getDate(2)) and then get the date from there, but i cant find a suitable date method for that
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i improved a tiny bit, but now i cant get it at all:

and i get: Sexta-feira, 14 Mar�o 1969
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i just read an edit about long, so its case solved:
now i have:


thanks a lot to all who tried to help me
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!