Kinjal Raval
Kinjal Raval

Reputation: 19

wrong timezone in java

i have a method in which i am trying to fetch system's local time zone, but i am always get "Coordinated Universal Time" as a time zone where i am expecting it to be "India Standard Time"

code is as below

Calendar c = Calendar.getInstance();
              
 //get local TimeZone by
TimeZone timezone = c.getTimeZone();
              
System.out.println("Local TimeZone is : " + timezone.getDisplayName());
               

Please let me know how to fetch user's system timezone dynamically Thanks!

Upvotes: 0

Views: 1066

Answers (3)

Mahesh
Mahesh

Reputation: 355

This will give the local time zone:

import java.time.ZoneId

ZoneId zone = ZoneId.systemDefault();

Upvotes: 2

deHaar
deHaar

Reputation: 18558

You can try to use java.time (recommended nowadays anyway and definitely to be preferred over java.util):

public static void main(String[] args) {
    // first, define a locale for your output
    Locale locale = Locale.ENGLISH; // you can use your system default here, too
    // then get the zone id of the system
    ZoneId sysZoneId = ZoneId.systemDefault();
    // and use one of its names (full, narrow, short) in the defined language
    String sysZoneFullName = sysZoneId.getDisplayName(TextStyle.FULL, locale);
    String sysZoneShortName = sysZoneId.getDisplayName(TextStyle.SHORT, locale);
    String sysZoneNarrowName = sysZoneId.getDisplayName(TextStyle.NARROW, locale);
    // get the offset from UTC the zone currently has
    OffsetDateTime odt = OffsetDateTime.now(sysZoneId);
    ZoneOffset sysOffset = odt.getOffset();
    // then prepare some output (could be done in a better way ;-) )
    int offsetInSeconds = sysOffset.getTotalSeconds();
    char direction = (offsetInSeconds < 0) ? '-' : '+';
    // then output each zone representation (full, narrow, short) and the offset from UTC
    System.out.printf("%s / %s / %s has an offset of %s%d hours from UTC",
            sysZoneFullName, sysZoneNarrowName, sysZoneShortName, 
            direction, offsetInSeconds / 3600);

On my system (located in Germany), this outputs

Central European Time / Europe/Berlin / CET has an offset of +2 hours from UTC

Check out the different options of getting the name of a ZoneId for your code. There are STANDALONE versions for each name representation, too. I hope you find the one you need.

UPDATE

Unfortunately, IST is none of the representations provided by ZoneId. Instead, you will get IT or India Time if your systemDefault() is "Asia/Kolkata". I have tried to define that zone and print its representations:

public static void main(String[] args) {
    Locale locale = Locale.ENGLISH;
    ZoneId ist = ZoneId.of("Asia/Kolkata");
    System.out.println(ist.getDisplayName(TextStyle.FULL, locale));
    System.out.println(ist.getDisplayName(TextStyle.FULL_STANDALONE, locale));
    System.out.println(ist.getDisplayName(TextStyle.NARROW, locale));
    System.out.println(ist.getDisplayName(TextStyle.NARROW_STANDALONE, locale));
    System.out.println(ist.getDisplayName(TextStyle.SHORT, locale));
    System.out.println(ist.getDisplayName(TextStyle.SHORT_STANDALONE, locale));
}

and it outputs

India Time
India Time
Asia/Kolkata
IT
IT
IT

Better than nothing, you can use the output as a condition for printing what you need.

Upvotes: 3

Kinjal Raval
Kinjal Raval

Reputation: 19

I am able to get device time zone dynamically by below code

Calendar c = Calendar.getInstance();
              
TimeZone timezone = c.getTimeZone();

TimeZone.setDefault(null);

System.out.println("Local TimeZone is : " + timezone.getDisplayName());

Upvotes: 0

Related Questions