0

Can anyone please help me how to convert the given time into CST timezone.

Input timezone will change dynamically, so i want to convert the given timzone into CST and then need to get the hour from that CST timezone in apex.

  • 2
    Salesforce natively stores datetimes in UTC and converts them into the user's timezone when displayed. Attempting to implement your own time-zone management is rarely an ideal solution. What's your overall objective? – David Reed Jan 7 '19 at 23:38
  • if user is entering a time + timezone, convert that to GMT then back to CST; see Timezone class – cropredy Jan 8 '19 at 0:21
  • @cropredy - i am expecting the solution similar to this. Can you please provide the logic for that – user5558 Jan 8 '19 at 4:29
2

Assuming you just had a DateTime and wanted to the the CST hour value then you can use the DateTime.format() method.

DateTime inputDate = DateTime.now();
System.debug(inputDate.format('HH', 'CST'));

The 'HH' here is that you want just the hours component.

The 'CST' is a Java TimeZone ID.

You will need to take care that the input DateTime has a defined timezone that matches the uses input so the conversion occurs as expected. For instance, if you just use the newInstance() methods it will create the DateTime in the users local time zone.


I've gone through a more elaborate version of this in Convert time from a different timezone other than local to GMT. That uses the TimeZone class to get the UTC offsets and requires more work than a direct format().

1

This is my utility class to go from input of a valid timezoneId, date, and time object to GMT

/*
    getDateTimeGmt  : Returns a datetime in GMT from a timezoneId, date and time object
*/
public static Datetime getDateTimeGmt(String tzId, Date d, Time t) {
    DateTime    res;
    Timezone    tz          = tzId == null ? UserInfo.getTimeZone() : Timezone.getTimeZone(tzId);
    DateTime    dt          = DateTime.newInstanceGmt(d == null ? Util.today : d,
                                                      t == null ? Time.newInstance(0,0,0,0) : t);
    Integer     offsetMs    = tz.getOffset(dt);                 // #ms offset from datetime in Tz to GMT
    Integer     offsetMins  = offsetMs / (1000 * 60);
    res = dt.addMinutes(-1 * offsetMins);

    return res;
} 

/* today - allows testmethods to coerce 'today' for predictable tests */
public static Date today        {get {return today == null ? Date.today() : today;} set;}

Then you can convert the result to the desired timezone America/Chicago using Daniel Ballinger's technique of datetime.format(..)

The testmethod below illustrates how to use the method

@IsTest
private static void testGetDateTimeGmt() {
    String  tz;
    Date    d;
    Time    t;

    //  Verify Pacific Std (January) and DST (May)
    System.assertEquals(DateTime.newInstanceGmt(2040,1,1,19,0,0),   Util.getDateTimeGmt('America/Los_Angeles',Date.newInstance(2040,1,1),Time.newInstance(11,0,0,0)),'sb offset by -8 hours - not DST');
    System.assertEquals(DateTime.newInstanceGmt(2040,5,1,18,0,0),   Util.getDateTimeGmt('America/Los_Angeles',Date.newInstance(2040,5,1),Time.newInstance(11,0,0,0)),'sb offset by -7 hours -  DST');
    System.assertEquals(DateTime.newInstanceGmt(2040,5,2,4,0,0),    Util.getDateTimeGmt('America/Los_Angeles',Date.newInstance(2040,5,1),Time.newInstance(21,0,0,0)),'sb offset by -7 hours -  DST but GMT is next day');

    // Go the other way - Europe/Paris
    System.assertEquals(DateTime.newInstanceGmt(2040,1,1,10,0,0),   Util.getDateTimeGmt('Europe/Paris',Date.newInstance(2040,1,1),Time.newInstance(11,0,0,0)),'sb offset by +1 hours - not DST');
    System.assertEquals(DateTime.newInstanceGmt(2040,6,1,9,0,0),    Util.getDateTimeGmt('Europe/Paris',Date.newInstance(2040,6,1),Time.newInstance(11,0,0,0)),'sb offset by +2 hours - DST');

    // null inputs for date and time
    Util.today = Date.newInstance(2040,7,1);
    System.assertEquals(DateTime.newInstanceGmt(2040,7,1,7,0,0),        Util.getDateTimeGmt('America/Los_Angeles',null,null),'sb offset by -7 hours - not DST');

    // null input for tzId - use running user's TZ in lieu
    d   = Date.newInstance(2040,1,1);
    t   = Time.newInstance(11,0,0,0);
    DateTime expectedResult = Util.getDateTimeGmt(UserInfo.getTimeZone().getId(),d,t);
    System.assertEquals(expectedResult,     Util.getDateTimeGmt(null,d,t),'should match running user\'s TZ offset from GMT - not DST');
}

Example:

Go from user entered India/Calcutta on 2040-01-01 at 1500h to Central time HOUR

Datetime gmt = Util.getDateTimeGmt('India/Calcutta',Date.newInstance(2040,1,1),
                                   Time.newInstance(15,0,0,0));
String centralTimeHour = gmt.format('HH','America/Chicago');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.