• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting time

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic