Specify the TimeZone explicitly for Gregorian Calendar in Java


To specify the TimeZone explicitly, use the getTimeZone() method of the TimeZone class. For Locale and TimeZone, we have imported the following packages.

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

Let us specify for timezone America/New_York.

GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"), Locale.US);

The following is an example.

Example

 Live Demo

import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
public class Demo {
   public static void main(String[] a) {
      GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("America/New_York"), Locale.US);
      System.out.println(cal);
   }
}

Output

java.util.GregorianCalendar[time=1542643323673,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,start

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 27-Jun-2020

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements