I am having date field in the UI. I have written code as per below

    Datetime XXX = (Datetime)FieldName;
    yyy= XXX.format('yyyyMMdd');

But after executing the batch date is decremented by 1 automatically Can anyone suggest some idea??

Thanks in advance.

  • While formatting the date format ' the date is decremented by 1 day '... – Keerthi Jan 6 '20 at 10:11
  • What's your timezone? – RubenDG Jan 6 '20 at 10:54
  • America/Newyork – Keerthi Jan 6 '20 at 10:58

Datetime.format() convert the Datetime to your local time zone, while a System.debug() of a Datetime always shows it in ISO format UTC (GMT+0).
Since New York is at GMT-5, if the Datetime in UTC is between 00:00 and 04:59, calling .format() method will return the day before.
In order to get the same date you see by System.debug(Datetime), you have to use .formatGMT


Datetime d1 = System.now();
String sFormat = d1.format('yyyy/MM/dd HH:mm:ss');
String sFormatGMT = d1.formatGMT('yyyy/MM/dd HH:mm:ss');
System.debug(d1); // DEBUG|2020-01-06 11:13:21 because it always shows the datetime in UTC
System.debug(sFormat); // DEBUG|2020/01/06 12:13:21 because I'm at GMT+1
System.debug(sFormatGMT); // DEBUG|2020/01/06 11:13:21 because I forced the GMT time zone

If you want to convert the date to a specific time zone you could use .format(String pattern, String timezone). The documentation states:

Converts the date to the specified time zone and returns the converted date as a string using the supplied Java simple date format. If the supplied time zone is not in the correct format, GMT is used.

About the timezone parameter you should use the full name, not the abbreviation (emphasis mine):

Valid time zone values for the timezone argument are the time zones of the Java TimeZone class that correspond to the time zones returned by the TimeZone.getAvailableIDs method in Java. We recommend you use full time zone names, not the three-letter abbreviations.


The reason for this is the DateTime field will always displayed with GMT timezone in UI. To show the data is UI with current user timezone, you can have the data in a string and display.

String timeZone = UserInfo.getTimeZone().getID();
Datetime dateinGMT= FieldName;
Datetime d1=Datetime.valueOf(dateinGMT);        
string str= d1.format('dd MMMM YYYY h:mm a ');

The str can be displayed in the UI. Which takes the current user's timezone

  • As per your solution, the solution i got is d1 = 1/5/2020 str = 1/4/2020. Still same problem is residing. – Keerthi Jan 6 '20 at 10:32
  • "the DateTime field will always displayed with GMT timezone in UI": that's not quite right. DateTime fields are stored in GMT. When shown in the standard Salesforce UI, they're converted to the user's timezone. Ruben's answer below discusses the conversions that take place when logging data. – David Reed Jan 6 '20 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.