Specify the TimeZone explicitly for Gregorian Calendar in Java

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 17-Dec-2018 16:35:10
Advertisements