Home arrow static arrow Java Programming [Archive] - Getting the current date exactly as machine
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Getting the current date exactly as machine
This topic has 15 replies on 2 pages.    1 | 2 | Next »

Posts:51
Registered: 8/17/00
Getting the current date exactly as machine  
Jul 22, 2004 5:58 AM



 
Dears,
I want to get the current date/time in my web application on SUNONE 7, I recognized that the time I receive is 1 hour after the real time I have on the machine.

I just don't want to worry about any locale problems, I just want to get the date/time in the application server machine as it is, as it is already controlled by the SYS ADMIN.

My code I use is:
new Timestamp( new Date().getTime() )

Any suggestion??????????????????
 

Posts:27
Registered: 8/21/03
Re: Getting the current date exactly as machine  
Jul 22, 2004 8:46 AM (reply 1 of 15)



 
Try to Use

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

Hope it will work :)

 

Posts:18,384
Registered: 21.03.00
Re: Getting the current date exactly as machine  
Jul 22, 2004 10:38 AM (reply 2 of 15)



 
That will give the same result.

/Kaj
 

Posts:3,055
Registered: 18/06/98
Re: Getting the current date exactly as machine  
Jul 22, 2004 10:47 AM (reply 3 of 15)



 
Problems related to Daylight Summer Time and locales... May anyone help him?
Please, mr. Ashraf Fouad, post the details:

- JDK version (as printed by java -version)
- Operating system (Windows? Unix? Linux? What version?)
- Value of the environment variable TZ (if it is an Unix/Linux environment) and other environment variables related to time zones and locales (like LANG)
 

Posts:11,200
Registered: 7/22/99
Re: Getting the current date exactly as machine  
Jul 22, 2004 11:26 AM (reply 4 of 15)



 
the real time I have on the machine

By this do you mean the hardware clock's time (which is likely UTC, Coordinated Universal Time) or the user's time with time zone offset and daylight savings time taken to consideration?
 

Posts:51
Registered: 8/17/00
Re: Getting the current date exactly as machine  
Jul 26, 2004 2:00 AM (reply 5 of 15)



 

- JDK version = 1.4.1_03

- Operating system = Solaris 5.9 SPARC

- Value of the environment variable TZ TZ=GMT-3

- LANG --------not specified-----------------

What I need is the hardware clock only.
 

Posts:11,200
Registered: 7/22/99
Re: Getting the current date exactly as machine  
Jul 26, 2004 2:34 AM (reply 6 of 15)



 
What I need is the hardware clock only.

You already have it; the problem is probably just with how it's formatted. This code formats the current date and time in UTC, see if the time it gives you is what you want:
DateFormat format = DateFormat.getDateTimeInstance();format.setTimeZone(TimeZone.getTimeZone("UTC"));String date = format.format(new Date());System.out.println(date);
 

Posts:3,055
Registered: 18/06/98
Re: Getting the current date exactly as machine  
Jul 26, 2004 9:38 AM (reply 7 of 15)



 
Thank you for the explanation, jsalonen.

If Mr. Fouad were in Egypt his clock's machine was set wrong (it must be GMT + 2); if in Greenland (GMT - 3) I believe that's summertime now. (Other regions of the world with GMT - 3, like Argentina and Brazil, are in winter now. )
 

Posts:11,200
Registered: 7/22/99
Re: Getting the current date exactly as machine  
Jul 26, 2004 10:38 AM (reply 8 of 15)



 
According to this reference the time zone of Cairo with DST is GMT plus three hours, so the TZ setting of GMT-3 is correct.. (it seems that to set the time zone to GMT+03:00, TZ needs to be set to GMT-3 (???) SunOS 5.8 on sparc) http://www.timeanddate.com/worldclock/city.html?n=53
 

Posts:51
Registered: 8/17/00
Re: Getting the current date exactly as machine  
Jul 26, 2004 1:26 PM (reply 9 of 15)



 
I'm from Cairo but the project is in KSA, the GMT is correct.

I think there should be simple way for getting hardware clock without getting into all these details, as any server clock in repectable company is maintained by gauarnteed systems so I want to depend on it.
 

Posts:37,103
Registered: 3/30/99
Re: Getting the current date exactly as machine  
Jul 26, 2004 1:29 PM (reply 10 of 15)



 
I think there should be simple way for getting
hardware clock without getting into all these details,
as any server clock in repectable company is
maintained by gauarnteed systems so I want to depend
on it.

You do need to get into the details though. You need to understand how timezones work, what's in the system clock, etc.

If the system is configured correctly, then things will work. But you still need to understand what's going on.

As for assuming that the server is guaranteed to be correct, that's a dangerous assumption to make.
 

Posts:11,200
Registered: 7/22/99
Re: Getting the current date exactly as machine  
Jul 26, 2004 1:45 PM (reply 11 of 15)



 
I think there should be simple way for getting
hardware clock without getting into all these details,

There is. System.currentTimeMillis() returns the current system clock time in milliseconds.

These details are required only for proper formatting of the time -- the same instance of Date with the same number of milliseconds since 1.1.1970 00:00:00 GMT can have any hour depending on the time zone used.
 

Posts:51
Registered: 8/17/00
Re: Getting the current date exactly as machine  
Jul 26, 2004 1:59 PM (reply 12 of 15)



 
u r correct, can u suggest me best reference in this issue
 

Posts:37,103
Registered: 3/30/99
Re: Getting the current date exactly as machine  
Jul 26, 2004 2:22 PM (reply 13 of 15)



 
u r correct, can u suggest me best reference in this
issue

I don't know about "best", but look at the javadocs for Date, Calendar, and TimeZone, and maybe Locale. Also, Date has a couple of useful links:

http://tycho.usno.navy.mil/
http://tycho.usno.navy.mil/systime.html

Additionally, try man date or info date on your Solaris box and see where that leads.

Finally, you can just google for some of the keywords, like "java date timezone DST".

If everything is set up right, then the system clock should just count the number of millis elapsed since 1/1/1970 00:00:00.000 GMT. This will be the same regardless of what timezone you're in. When I travel, and I change timezones on my laptop, I'm not changing the system clock.

Then you've got your OS's notion of what TZ it's in. As part of that, or perhaps in addition to it, is information as to whether it automatically adjusts for DST (summertime/wintertime). If the TZ or the DST adjustment is wrong. On Linux/Unix, there are files that have info about each TZ. I think Linux keeps them in /usr/share/zoneinfo. Solaris probably has a similar setup. Those files contain information about the offest from GMT, whether DST is in effect and when, etc. If the file that your system is using has an error in it, then you'll see the incorrect time.

Then there's how Java determines the TZ, DST offest, etc. The details should be in one of those javadoc APIs. I think when you give it a named TZ, it's supposed to try to use the corresponding one from the system, and if it can't find it it uses it's own. Or maybe it just always uses its own. I believe the API does come with a bunch of TZ objects similar to the TZ files you'd find in .../zoneinfo or wherever. Those seem to get updated with every release, so it's possible there's a bug in Java's TZ that corresponds to your timezone.

Ask your sysadmin which TZ the system is using, and then see if you can find out its rules about DST. Also look into the details of the TZ object you get from Java when you request the appropriate one. I think just be calling toString you can see the offest from GMT and some info about its DST handling.
 

Posts:3,055
Registered: 18/06/98
Re: Getting the current date exactly as machine  
Jul 26, 2004 2:41 PM (reply 14 of 15)



 
Comment:

I live in Brazil and we usually know about the exact starting and ending days of Daylight Savings Time (here is the "horário de verão", a literal translation of the English word "summertime", although it actually starts in Spring) within just a few weeks before the fact (you can't write an algorithm that says exactly when the DST will start or end; when the Pope visited us they delayed the ending of the DST by one week.).

The TZ settings (or the Windows DST settings) and the Java DST settings usually go astray.

I don't know how Java interprets the TZ settings in Solaris machines. Maybe reading the SCSL source code...

So several businesses in Brazil usually turn off the automatic DST settings (there is a TZEditor for Windows, but it changes a lot of entries in the registry and it is not easily deployable...) and manually adjust each server to the desired time when the DST starts and ends.

The problem is that the servers are not synchronized with UTC when the DST is on; it proves to be a problem if you want to make our servers synchronized with an atomic clock (a NTP server), for instance.
 
This topic has 15 replies on 2 pages.    1 | 2 | Next »