Java.util.GregorianCalendar.setTimeZone() Method


Advertisements


Description

The java.util.GregorianCalendar.setTimeZone(TimeZone zone) method sets the time zone with the given time zone value.

Declaration

Following is the declaration for java.util.GregorianCalendar.setTimeZone() method

public void setTimeZone(TimeZone zone)

Parameters

  • zone -- the given time zone.

Return Value

This method does not return a value.

Exception

  • NA

Example

The following example shows the usage of java.util.GregorianCalendar.setTimeZone() method.

package com.tutorialspoint;

import java.util.*;

public class GregorianCalendarDemo {

   public static void main(String[] args) {

   // create a new calendar
   GregorianCalendar cal =
   (GregorianCalendar) GregorianCalendar.getInstance();

   // print the current date and time
   System.out.println("" + cal.getTime());

   // set to another time zone
   cal.setTimeZone(TimeZone.getTimeZone("GMT"));
   System.out.println("New Time Zone:" + cal.getTimeZone().getDisplayName());

   }
}

Let us compile and run the above program, this will produce the following result:

Fri May 18 13:48:30 EEST 2012
New Time Zone:Greenwich Mean Time

java_util_gregoriancalendar.htm

Advertisements