• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting time  RSS feed

 
srini Raman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
In one of the solaris machines which we have, the unix command date returns correct date and locale . The java prgm with System.currentTimeMillis() gives the wrong time...It takes the IST(Indian Standard Time) and adds 5.30 and gives the time (Though there is no need for such conversion...If its GMT then it requires this conversion.But the time is already in IST)...The machine is set to IST locale only. What may be the problem??
Thanks,
Srini
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.currentTimeMillis() is indeed supposed to give you UTC (==GMT), whatever your actual location. I doubt if the JVM for Solaris has a bug in this (though maybe you could check the Sun Bug Database), so it sounds like a problem with the configuration of your Solaris machine.

Note that System.currentTimeMillis() is rarely used for getting the absolute time, because doing so is rather low-level and non-Java-like. It is usually used for time differences. It is more common to use Date for absolute time.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does your Java program look like? How does it print the current time?

As you can read in the API documentation, System.currentTimeMillis() returns the number of milliseconds that have passed since a specific point in time: January 1, 1970, 00:00 UTC.

You can get a java.util.Date object that represents the current date and time simply by creating a new Date object with the no-args constructor. If you convert this to a String by calling toString() (implicitly or explicitly), you will see the date and time in the timezone that the operating is set to.

If you want to display it in a different timezone, use a DateFormat object to convert the Date to a String and set the desired timezone on the DateFormat object. For example:

The output of this is (on my system):

Tue Aug 07 13:14:50 CEST 2007
2007-08-07 16:44:50 IST
 
srini Raman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

Actually I am using as following

new java.util.Date( System.currentTimeMillis());

This gives my time in my locale correctly in windows ...In some solaris machine I am not getting it correctly. I will try to avoid System.currentTimeMillis and try to use new Date() directly to overcome the problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!