+ Start a Discussion
Saikishore Reddy AengareddySaikishore Reddy Aengareddy 

Date Time methods Issue

// Datetime on July 03, 2012 at 1:06:08 AM in local timezone.

Datetime startTime = Datetime.newInstance(2012, 7, 3, 1, 6, 8);

 

// Datetime on July 5, 2012 at 4:06:08 PM in GMT timezone.


Datetime endTime = Datetime.newInstanceGMT(2012, 7, 5, 16, 6, 8);

 

system.debug('startTime>>> '+startTime+' EndTime>>> '+endTime);

 

Output:
17:34:39.118 (118314000)|SYSTEM_METHOD_ENTRY|[30]|System.debug(ANY)
17:34:39.118 (118323000)|USER_DEBUG|[30]|DEBUG|Start Time>>>2012-07-03 08:06:08    End Time>>2012-07-05 16:06:08

 

DateTime.newInstance is returning datetime in GMT where as DateTime.newInstanceGMT is returning in my local timezone. As per the Documentation newinstance should return datetime in local time zone and newInstanceGMT should return datetime in GMT.

 

Am I missing something here or my usage is incorrect. Please help me understand.

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
SeAlVaSeAlVa

Is the other way around. 

 

newInstanceGMT: Constructs a DateTime from the specified date and time in the GMT time zone.

 

What I believe that means is that you provide datetime in GMT timezone, but does not make it return it in that timezone. 

 

(It always returns the datetime in the timezone, just ease you the task to create it)