How to convert GMT to locale dateTime?

I want to convert the GMT dateTime to locale dateTime as System.now(). But after converting the format when I am trying to convert that string into dateTime again it is giving me the GMT dateTime.

Answer

Your question is not very clear to me, but my two possible interpretations are you want to format the current Datetime according to your locale, or you want to adjust the System.now() result to account for the local time zone.

If you just want a locale-specific formatted string to represent the current Datetime, use:

String formatted = Datetime.now().format();

If you are trying to get the current Datetime in the local time zone, you can use the following code:

Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);

Attribution
Source : Link , Question Author : Stuart Cole , Answer Author : Przemysław Długoszewski-Tamoń

Leave a Comment