Newer Version Available
TimeZone Class
Namespace
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 TimeID: America/Los_Angeles
Offset: -25200000
Offset: -28800000
String format: America/Los_Angeles
This second example shows how to create a time zone for the New York time zone and get the offset of this time zone to the GMT time zone. The example uses two dates to get the offset from. One date is before DST, and one is after DST. In 2000, DST ended on Sunday, October 29 for the New York time zone. Because the date occurs after DST ends, the offset on the first date is –5 hours to GMT. In 2012, DST ended on Sunday, November 4. Because the date is within DST, the offset on the second date is –4 hours.
1// Get the New York time zone
2Timezone tz = Timezone.getTimeZone('America/New_York');
3
4// Create a date before the 2007 shift of DST into November
5DateTime dtpre = DateTime.newInstanceGMT(2000, 11, 1, 0, 0, 0);
6system.debug(tz.getOffset(dtpre)); //-18000000 (= -5 hours = EST)
7
8// Create a date after the 2007 shift of DST into November
9DateTime dtpost = DateTime.newInstanceGMT(2012, 11, 1, 0, 0, 0);
10system.debug(tz.getOffset(dtpost)); //-14400000 (= -4 hours = EDT)This next example is similar to the previous one except that it gets the offset around the boundary of DST. In 2014, DST ended on Sunday, November 2 at 2:00 AM local time for the New York time zone. The first offset is obtained right before DST ends, and the second offset is obtained right after DST ends. The dates are created by using the DateTime.newInstanceGMT method. This method expects the passed-in date values to be based on the GMT time zone.
1// Get the New York time zone
2Timezone tz = Timezone.getTimeZone('America/New_York');
3
4// Before DST ends
5DateTime dtpre = DateTime.newInstanceGMT(2014, 11, 2, 5, 59, 59); //1:59:59AM local
6system.debug(tz.getOffset(dtpre)); //-14400000 (= -4 hours = still on DST)
7
8// After DST ends
9DateTime dtpost = DateTime.newInstanceGMT(2014, 11, 2, 6, 0, 0); //1:00:00AM local
10system.debug(tz.getOffset(dtpost)); //-18000000 (= -5 hours = back one hour)TimeZone Methods
The following are methods for TimeZone.
getDisplayName()
Signature
public String getDisplayName()
Return Value
Type: String
Versioned Behavior Changes
In API version 45.0 and later, getDisplayName displays Daylight Savings Time appropriately when daylight savings are in effect. For example, British Summer Time is displayed for Europe/London and Pacific Daylight Time for America/Los_Angeles.
getTimeZone(timeZoneIdString)
Signature
public static TimeZone getTimeZone(String timeZoneIdString)
Parameters
- timeZoneIdString
- 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 = TimeZone.getTimeZone('America/Los_Angeles');
2String tzName = tz.getDisplayName();
3System.assert(tzName.equals('(GMT-08:00) Pacific Standard Time (America/Los_Angeles)') ||
4 tzName.equals('(GMT-07:00) Pacific Daylight Time (America/Los_Angeles)'));toString()
Signature
public String toString()
Return Value
Type: String