Calendar
Get current TimeZone using Calendar
In this example we shall show you how to get the current TimeZone using Calendar. The Calendar is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR
, MONTH
, DAY_OF_MONTH
, HOUR
. To get the current TimeZone using Calendar one should perform the following steps:
- Use
getInstance()
API method of Calendar, in order to get a Calendar object, using the default time zone and locale. - Use
getTimeZone()
API method of Calendar, that returns the TimeZone object associated with this calendar. The TimeZone represents a time zone offset, and also figures out daylight savings. - Use
getDisplayName()
API method of TimeZone to get a long standard time name of this TimeZone suitable for presentation to the user in the default locale,
as described in the code snippet below.
package com.javacodegeeks.snippets.core; import java.util.Calendar; import java.util.TimeZone; public class GetCurrentTimeZoneUsingCalendar { public static void main(String[] args) { Calendar c = Calendar.getInstance(); //get current TimeZone using TimeZone tz = c.getTimeZone(); System.out.println("Current TimeZone is : " + tz.getDisplayName()); } }
Output:
Current TimeZone is : Eastern European Time
This was an example of how to get the current TimeZone using Calendar in Java.