Monday 23 July 2012

Get current TimeZone using Java Calendar

This example shows how to get current TimeZone using getTimeZone method of Java Calendar class.


import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

public class GetCurrentTimeZone {

 public static void main(String[] args) {
  Calendar c = Calendar.getInstance();
  TimeZone tz = c.getTimeZone();
  ;
  String s = tz.getDisplayName();
  System.out.println("TIME ZONE in different formats:");

  /**
   * Returns a name of this time zone suitable for presentation to the
   * user in the default locale. This method returns the long name, not
   * including daylight savings.
   **/
  System.out.println("tz.getDisplayName()=" + tz.getDisplayName());

  /**
   * Returns a name of this time zone suitable for presentation to the
   * user in the specified locale. This method returns the long name, not
   * including daylight savings.
   **/
  System.out.println("tz.getDisplayName(Locale.FRENCH)="
    + tz.getDisplayName(Locale.FRENCH));
  /**
   * Returns a name of this time zone suitable for presentation to the
   * user in the default locale.if first parameter 'daylight' is true,
   * returns the daylight savings name.The second parameter 'style' can
   * have either LONG or SHORT.
   **/
  System.out.println("tz.getDisplayName(true,TimeZone.SHORT)="
    + tz.getDisplayName(true, TimeZone.SHORT));
  System.out.println("tz.getDisplayName(false,TimeZone.SHORT)="
    + tz.getDisplayName(false, TimeZone.SHORT));

  /**
   * Returns a name of this time zone suitable for presentation to the
   * user in the specified locale.if 'daylight' true, returns the daylight
   * savings name.The 'style' can have either SHORT or LONG.
   **/
  System.out
    .println("tz.getDisplayName(true,TimeZone.LONG,Locale.CANADA)="
      + tz.getDisplayName(true, TimeZone.LONG, Locale.CANADA));
  System.out
    .println("tz.getDisplayName(false,TimeZone.LONG,Locale.CANADA)="
      + tz.getDisplayName(false, TimeZone.LONG, Locale.CANADA));

 }

}


The output is

TIME ZONE in different formats: tz.getDisplayName()=India Standard Time tz.getDisplayName(Locale.FRENCH)=Heure normale d'Inde tz.getDisplayName(true,TimeZone.SHORT)=IDT tz.getDisplayName(false,TimeZone.SHORT)=IST tz.getDisplayName(true,TimeZone.LONG,Locale.CANADA)=India Daylight Time tz.getDisplayName(false,TimeZone.LONG,Locale.CANADA)=India Standard Time



The links that may help you:

No comments:

Post a Comment