function is not working as appropriate – Salesforce Apex Issue

I am using in a controller in my application. Later I have so many calculations with it.
But Date time calculations are not as per timezone of my client. Why?

Solution: Yes. It will happens because, returns the DateTime in GMT time zone, whereas and are the methods of DateTime class, which return the local time zone of current logged in user.

But, one thing we have to find is that will return DateTime and all other methods of DateTime will be string. It means, will output as string, which is calculated with logged in user’s time zone.

Datetime.NewInstance() will create a datetime in SYSTEM’s time zone.
Datetime.NewInstanceGmt() will create a datetime in GMT time zone.

String day = string.valueOf(;
String month = string.valueOf(;
String hour = string.valueOf(;
String minute = string.valueOf(;
String second = string.valueOf(;
String year = string.valueOf(;

String strTime = year+’-‘+month+’-‘+day+’ ‘+hour+’:’+minute+’:’+second ;


Output 1: 2016-11-25 20:46:47
Output 2: 2016-11-25 15:16:47

Related posts