• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to get System Timezone?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My system is configured to be in BST Timezone. But
spZone = java.util.TimeZone.getDefault(); gives me "GMT".

What is the command to get the system timezone i.e. BST?

Thanks in advance,
Diksha
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On my Ubuntu804 system using JDK1.6.0_07, BST is not "British Standard Time" but "Bangladesh Time" .

Please run


Also, when I run
I get
Greenwich Mean Time
GB
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Sabre:

Also, when I run
I get
Greenwich Mean Time
GB



It is interesting that when I run that code on Windows XP SP3 using 1.6.0_07 I get

Greenwich Mean Time
Europe/London
 
Diksha Neel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James! Thanks for the reply but what I am looking for is printing the SYSTEM Timezone...whatever be it -BST/EST/IST etc.

So if my System in configured to be in say XYZ timezone, I wish my Java code to be able to print this XYZ timezone.

What is the command for this? I am using Java 1.4.2.

Thanks,
Diksha
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry but I don't think I can help. The only way I know of accessing the timezone is though the TimeZone class and if this does not give you the timezone ID in the format you want then nothing obvious and portable springs to mind.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Diksha, I guess that all you need is something like



Regards
 
Diksha Neel
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maurizio !

This also returns GMT...and not the actual system timezone.

Thanks,
Diksha
 
Ranch Hand
Posts: 113
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Diksha, i am sorry if i understood it wrongly i tried the code given as
----------------
TimeZone tz = Calendar.getInstance().getTimeZone();
System.out.println(tz.getDisplayName());// (i.e. Moscow Standard Time)
System.out.println(tz.getID());
-----------------
and for me the output was
---------------------
India Standard Time
Asia/Calcutta
---------------------
then i went back and changed the time zone to Singapor and got the followng output
----------------------
Singapore Time
Asia/Singapore

----------------------
it is taking the time zone dynamically and shownig it so where is your problem?

one more thing i added following line of code

System.out.println(tz.getTimeZone(tz.getID()));//will this help ?


[ October 24, 2008: Message edited by: Tanu Gulati ]
[ October 24, 2008: Message edited by: Tanu Gulati ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also post similar type of Java example to How to get other TimeZone time by JAVA.

http://binodjava.blogspot.com/2009/05/how-to-get-other-timezone-time-by-java.html

Thanks,

Binod Suman

http://binodsuman.blogspot.com
 
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way: It's not "British Standard Time" which we suffered from about 1969-1971. BST means British Summer Time. What in the States they call daylight saving time.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i had issues with this previously....

when i run the


its prints GMT for me...

output
Greenwich Mean Time
Europe/London
09:50:07


but if i use simpledateformat...it prints correct ,,,



its prints the right time and time zone for me ...


output---Thu May 14 21:50:07 BST 2009
 
jittu goud
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you just want print the zone...then you might want to use

prints just BST
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jittu goud wrote:i had issues with this previously....

when i run the


its prints GMT for me...

output
Greenwich Mean Time
Europe/London
09:50:07


but if i use simpledateformat...it prints correct ,,,



its prints the right time and time zone for me ...


output---Thu May 14 21:50:07 BST 2009




Sorry for late reply.................

In order to get the short timezone like "BST" you could use the code below

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
TimeZone timeZone = Calendar.getInstance().getTimeZone();
System.out.println(timeZone.getDisplayName(false, TimeZone.SHORT));
This has worked for me.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be careful, Java will detect the system timezone as Europe/Paris if /etc/localtime is a symlink to /usr/share/zoneinfo/Europe/Paris, even if the contents of /usr/share/zoneinfo/Europe/Paris is UTC data!
 
Campbell Ritchie
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Namikaze Minato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I hope I didn't upset too many people by digging up such and old thread, but I had to share the solution I had been looking for such a long time...
 
Campbell Ritchie
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is quite all right to add new things to old threads. I see you have a simlink in /etc... That doesn't sound like a Windows® folder name. Is that problem specific to Linux, all Unix‑like systems, or particular distributions?
 
Namikaze Minato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this can happen in any Unix-like system. My specific case was CentOS, but the symlink may have come from some legacy program installing it...
/etc/localtime used to be a symlink in GNU/Linux a long time ago, and apparently Java still has code to handle /etc/localtime as a symlink
So from a sitation like this:
If you somehow ran this command:
This would overwrite /usr/share/zoneinfo/Europe/Paris with UTC information, effectively changing the system timezone to UTC for all applications except JVM-based ones!
 
Campbell Ritchie
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got a similar output but with London because I am in UK. I presume I could change that by altering the system timezone. I am using Fedora25 at present, which is very similar to CentOS.
 
Namikaze Minato
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a symlink in /etc/localtime ? If yes, does changing the system timezone (using the usual tools) change the symlink?
 
Campbell Ritchie
Marshal
Posts: 80226
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know. Haven't tried, not until I next go across the Channel and need to change timezones.
 
Hold that thought. Tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic