• Post Reply Bookmark Topic Watch Topic
  • New Topic

Where does currentTimeMillis get the time from  RSS feed

 
Robert A Kirkwood
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to work out where the System.currentTimeMillis retrieves the time from. Is it the system clock or does the Java VM have it's own clock or is it somewhere else?
I am returning a formatted date/time using new java.sql.Timestamp(System.currentTimeMillis())).
I have an application which returns one time but when I run the same java code manually I get another time (they appear to be an hour different). I wondered if one was picking up British Summer Time (BST) and the other Greenwich Mean Time (GMT). How can this be possible on the same NT server?
 
Jon Strayer
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a UNIX box the time zone is set on a per process basis. That is two processes running at the same time on the same box can report different times because their time zones are different.
How does this work on NT Server?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you say you can run the Java program "manually" - as opposed to what? What's the other way of running the program? Something like an application server maybe? It's possible that there's an environment configuration somewhere which controls the Locale.
One thing to look at: print out the raw value of System.currentTimeMillis(), as a long, not Timestamp. Look at this using both ways of running your program. If you get approximately the same result (differing only by however many milliseconds there are between program executions) then you know that currentTimeMillis() is not your probelm, but rather it's something about how the Timestamp is formatted (which is probably controlled by Locale). While you're at it, print out the value of Locale.getDefault(). If these are different (for different ways of running the program), that's probably the source of trouble. Either (a) figure out how to configure the app server / whatever to a different Locale, (b) invoke Locale.setDefault() within your program to impose a particular Locale on the entire JVM, or (c) create a DateFormat and use setLocale() on it to impose a particular Locale on that DateFormat without affecting the rest of the JVM. (You must then use this DateFormat whenever you want to display date information.)
Hope that helps...
 
Robert A Kirkwood
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can run the program manually and output the value of System.currentTimeMillis. The alternative way is to run an application which outputs the value of System.currentTimeMillis to an audit table.
I will try adding the additional prints to my program and see what output I get.
 
Avi Abrami
Ranch Hand
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Robert,
As far as I know, "System.currentTimeMillis()" returns the value of the system clock. However, what you see displayed is NOT necessarily the value of the system clock. Operating systems and java manipulate the raw value of the system clock for display purposes -- usually so as to adjust for daylight savings time.
So the actual behaviour depends on the operating system you are using as well as the concrete subclasses of "java.util.TimeZone" (since it is an abstract class).
Since you mention that you are using "java.sql.Timestamp", I assume a database is also involved in your equation, so the database may also be partially responsible for the behaviour you are seeing.
Based on the information you have supplied, there's not much more I can offer. If you would care to supply some more details of what you are trying to do, and what environment you are using, I may be able to offer some more.
Hope this has helped you.
Good Luck,
Avi.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!