Newer Version Available

This content describes an older version of this product. View Latest

TimeZone Class

Represents a time zone. Contains methods for creating a new time zone and obtaining time zone properties, such as the time zone ID, offset, and display name.

Namespace

System

Usage

You can use the methods in this class to get properties of a time zone, such as the properties of the time zone returned by UserInfo.getTimeZone, or the time zone returned by getTimeZone of this class.

Example

This example shows how to get properties of the current user’s time zone and displays them to the debug log.

1TimeZone tz = UserInfo.getTimeZone();
2System.debug('Display name: ' + tz.getDisplayName());
3System.debug('ID: ' + tz.getID());
4// During daylight saving time for the America/Los_Angeles time zone
5System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,10,23,12,0,0)));
6// Not during daylight saving time for the America/Los_Angeles time zone
7System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,11,23,12,0,0)));
8System.debug('String format: ' + tz.toString());

The output of this sample varies based on the user's time zone. This is an example output if the user’s time zone is America/Los_Angeles.  For this time zone, daylight saving time is -7 hours from GMT (-25200000 milliseconds) and standard time is -8 hours from GMT (-28800000 milliseconds).

Display name: Pacific Standard Time

ID: America/Los_Angeles

Offset: -25200000

Offset: -28800000

String format: America/Los_Angeles

TimeZone Methods

The following are methods for TimeZone.

getDisplayName()

Returns this time zone’s display name.

Signature

public String getDisplayName()

Return Value

Type: String

getID()

Returns this time zone’s ID.

Signature

public String getID()

Return Value

Type: String

getOffset(Datetime)

Returns the time zone offset, in milliseconds, of the specified date to the GMT time zone.

Signature

public Integer getOffset(Datetime date)

Parameters

date
Type: Datetime
The date argument is the date and time to evaluate.

Return Value

Type: Integer

Usage

The returned offset is adjusted for daylight saving time if the date argument falls within daylight saving time for this time zone.

Note

getTimeZone(String)

Returns the time zone corresponding to the specified time zone ID.

Signature

public static TimeZone getTimeZone(String Id)

Parameters

Id
Type: String
The time zone values you can use for the Id argument are any valid time zone values that the Java TimeZone class supports.

Return Value

Type: TimeZone

Example

1TimeZone tz =
2  TimeZone.getTimeZone(
3    'America/Los_Angeles');
4System.assertEquals(
5  'Pacific Standard Time',
6  tz.getDisplayName());

toString()

Returns the string representation of this time zone.

Signature

public String toString()

Return Value

Type: String