SFDC DateTime in local Time Zone

Posted by ExiaHuang on July 20, 2017

try this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Utility {
 
    public static Datetime getLocalDateTime(Datetime z)
    {    
        Datetime l = z.Date();
        l = l.addHours(z.hour());
        l = l.addMinutes(z.minute());
        l = l.addSeconds(z.second());
        
        return l;
    }
}
 
Datetime gmt = Datetime.Now();
Datetime local = Utility.getLocalDateTime(Datetime.Now());
System.Debug('GMT Time: ' + gmtNow);
System.Debug('Local Time: ' + local);

Here is an alternate method, straight forward.

1
2
3
4
5
6
Datetime current = System.now(); // returns date time value in GMT time zone.
 
Date currDate = current.date();
Date currTime = current.time();
 
Datetime local = datetime.newinstance(currDate,currTime); // This will return date time in my local time zone.