File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Help with System Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Help with System Time" Watch "Help with System Time" New topic

Help with System Time

Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
I'm having a lot of trouble with getting an accurate system time and I wonder if anyone has some thoughts on this:

In my app, I caclulate a timestamp value as follows and use a query to retrieve the cookie data from a DB.

As you can see, I get the java.util.Date() and time()... this should get the local system time. Works fine in my IDE.

however, on our hosted servers, the system time is correct as run by the bash Date command (i.e. not in the webapp!)--> gives correct local time there. However, in the webapp, the time is off by 8 hours!

here are the logs:

what I get from teh shell:

Is there another place where the servlet or system can get it's time? Is there a way to make this system independent -- keep in mind that I'll have to make sure teh DB is in sync.

thanks for any insight... hopefully someone has had this trouble too.

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

Are you getting the date returned in GMT instead? I believe that would be eight hours off from Pacific Time.

You could try using Calendar.getInstance().getTimeInMills() instead, and see if that gives you a time that matches the system.

In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
hmmm.... on further examination, I note that the time is actually GMT.

Not being a Linux expert, is it possible that the call to system time returns GMT, rather than the GMT +/- offset for the timezone? Could this be a Tomcat thing? (happy to close this thread and start a new one if so..)

Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
A Date is a moment in time. Timezone is irrelevant to identifying a particular moment in time.

Timezone is almost a rendering/formatting attribute. It only becomes relevant when you are wanting to display the moment in time to a human in a way they can understand. At that point you have to decide how to format it.

So you should use and store Date objects rather than Strings.

Pat Farrell

Joined: Aug 11, 2007
Posts: 4659

Date is a four letter word, at last in Java. Its really not a "date" at all, its a time, specifically number of milliseconds from a time long ago. As a result, if you add one (1) to a Date, most of the time it stays on the same date, but sometimes it changes to the next date.

A simple question: what timezone is the server set up to use? A lot of servers are set to UTC (aka GMT) because its the only zone that makes sense for international use.

The key in Java use of Date objects, and when you want to store a Date object in a RDBMS (say MySql), is that the Date object itself does not have a timezone. When you want to talk about the date with a timezone output, you specify the timezone in the format, not in the Date.

When you use MySql, you can set a timezone both system-wide and for your session. This tells the server how to interpret the date you feed it.
Rex Norm
Ranch Hand

Joined: Dec 11, 2010
Posts: 41
The timezone for the bash is GMT, from what I can tell. Tomcat (also residing on the Linux box) is Pacific -1 (whatever is 1 hr EARLIER than pacific)-- and the DB server is on Pacific (the create timestamps bear this out when writing to the DB). Pretty screwed up.

I'm currently working with the hosters to change the time on both servers to match and sync with some authority, so I don't get caught like this. It occurred to me that even if I make a call to an NRC time server, I have little control over the DB (since DB timestamps are used in my app). To make my app more portable, I think I need to first execute:

and use that in my calcs. That way, the way the server timevalue is in sync for both read and write. This solves my problem and is reasonably portable for the particular application of determining which cookie to dig up, and I don't have to worry about syncing time.

in fact, I was thinking I could use a prepared statement with the query as:

Tomcat (and therefore Webapp) Logfiles would be off though, and that **WOULD** make trouble shooting more complex... I'm really perplexed as to why the Tomcat timezone is not working... I'd think at least the logfile times would adjust.

I'm starting a thread in the Tomcat section to see what people know about timezone. thanks for the good insights.

I agree. Here's the link:
subject: Help with System Time
It's not a secret anymore!