+ Start a Discussion
narasimharaju monapati 6narasimharaju monapati 6 

Converting DatTime to any timezone from a picklist and again into GMT

Can anybody help me How to change the datetime field from any other timezone(Which is coming from a picklist) to GMT. My exact requirement is change the datetime field  to a timezone which is coming from the picklist and again converting into GMT. 

My code is below here

DateTime CurentTime = system.now();
String TimeZones = '(GMT+05:30) India Standard Time (Asia/Kolkata)';
List<String> lststrsplit = TimeZones.substring(12,TimeZones.length()).split('\\(',2);
        string strTimeZone = lststrsplit[1].substring(0,lststrsplit[1].length()-1);
        string strCurrentTime = CurentTime.format('YYYY-MM-dd HH:mm:ss', strTimeZone);
        Datetime dateCurrentTime = Datetime.valueof(strCurrentTime);
        string strGMTTimeZone = 'GMT';
        string strGMTtime = dateCurrentTime.format('MM-dd-YYYY HH:mm:ss', strGMTTimeZone);
        Datetime DateGMtTime = Datetime.valueofGMT(strCurrentTime);
system.debug( strCurrentTime);
ShashankShashank (Salesforce Developers) 
You can use custom settings to save the timezone picklist mapping with the exact time difference with GMT and fetch it based on the picklist to calculate the GMT time.