Best way to store Timestamps in files/databases?

Hi, I also need Timezone information for the timestamps - im sending the info in an xml-file between two servers, and finally insert information in a postgresql database. Best format for this? Maybe use long-values?Any help much appreciated!
[262 byte] By [Puzza] at [2007-11-27 8:46:56]
# 1
SQL type DATETIME, Java type java.sql.Timestamp.Use preparedStatement's ? parameter and ps.setTimestamp(1, myTimestamp); or whatever index, etc. are appropriate for your situation.
jverda at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi,

>

> I also need Timezone information for the timestamps -

No, you don't. Timestamp in Java and DATETIME in SQL are TZ-agnostic. TZ only comes into play when you're parsing a string into a date/timestamp or formatting a date/timestamp into a particular local time string.

jverda at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 3
Thanx for your answer!So, would I not gain anything by storing long values in the files and in db?
Puzza at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 4
> Thanx for your answer!> > So, would I not gain anything by storing long values> in the files and in db?You'd maybe save a bit of memory, but at the cost of losing the value of abstraction that Date, Timestamp, DATE, DATETIME, etc. provide.
jverda at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 5

I don't understand Timezones, I think.

At the fileserver I create a new Date() - current 'check' time for each file,

and I also retrieve File.lastModified() for each file.

If this File-server has a different TimeZone than the server to which the XML-files with information are sent, isnt it critical to save Timezone info along with date and time?

Puzza at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 6

> I don't understand Timezones, I think.

>

> At the fileserver I create a new Date() - current

> 'check' time for each file,

> and I also retrieve File.lastModified() for each

> file.

Which has no TZ information.

If someone in PDT, someone in EDT, someone in London, and someone in Tokyo create new Date() at the same instant, those Dates' contents will be identical.

> If this File-server has a different TimeZone than the

> server to which the XML-files with information are

> sent, isnt it critical to save Timezone info along

> with date and time?

No. Date just says how many millis have elapsed since a particular point in time. That value is not dependent on TZ.

M millis since the epoch is "right now", regardless of which TZ you're in. Once you factor in the TZ, "right now" might become 12:00 PDT, 14:00 CDT, etc.

jverda at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 7

So, I can use a timestamp in the xml-files and also use timestamp in db - and when displaying the timestamps, I can use a SimpleDateFormat to format the timestamp with any TimeZone I wish?

Also (thanx again for your patience!), I have the log-file dates. Here I parse out a String like '2007-12-10 22:10:00 0200' for each file. I guess timezone becomes an issue here (0200 for GMT+2 I think) . Im parsing to java.util.Date using SimpleDataFormatter - not sure really what to do with the timezone value? Suggestion for this?

Puzza at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...
# 8

> So, I can use a timestamp in the xml-files

I don't know what this means. If you mean you take a Timestamp's long millis value and shove it in the XML, without regard to TZ, and then extract it in the same way, yes, you can do that.

> and also

> use timestamp in db

Like I said: Create a Timestamp, use PreparedStatement.setTimestamp to push it into a DATETIME column of the DB, and you're golden. You're just marking an instant in time. Like an announcer on global TV is going, "NOW".

> - and when displaying the

> timestamps, I can use a SimpleDateFormat to format

> the timestamp with any TimeZone I wish?

Yup.

> Also (thanx again for your patience!), I have the

> log-file dates. Here I parse out a String like

> '2007-12-10 22:10:00 0200' for each file.

Okay, so you probably want a SimpleDateFormat that knows how to parse the "0200" on the end into appropriate TZ information to give you a TZ-agnostic date.

That is, that's +2 hrs from GMT, right? So when you parse that, you get a java.sql.Date that represents "that instant." It's 10:10 p.m. there, 5:10 p.m. somewhere else, 3:10 a.m. somewhere else, etc., but that doesn't matter. 10:10 p.m. here is exactly the same as 11:10 p.m. in the next TZ. Once I specify 10:10 p.m. here, I've also implicitly stated 00:10 CDT tomorrow in Chicago, etc.

Remember, "now" == X:00 PDT == X+2:00 CDT == X+3:00 EDT == X+ 11:30 or something India TZ, etc.

jverda at 2007-7-12 20:50:36 > top of Java-index,Java Essentials,Java Programming...