0

I'm trying to get the DateTime field values formatted to example below which is the Developer Console Query editor output.

e.g.

2017-01-04T06:00:00.000+0000

But when I used String.valueOf(datetimeToConvert) it gives a different format.

e.g.

2017-01-04 06:00:00

Is there a way to get it as a formatted string? without forced formatting? Also I wonder why those right padding zeros, char 'T', etc. Appreciate any help on this conversion. Thanks.

  • 1
    Char 'T', 'Z' and padding zeros specifies the time zone in the date format – RCS Jan 2 '17 at 9:26
2

Use JSON to serialize:

Account a = [Select CreatedDate From Account LIMIT 1];
System.debug(json.serialize(a.CreatedDate));

Or with string formatting:

System.debug(a.createdDate.format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX'));

02:28:12.31 (39198321)|USER_DEBUG|[2]|DEBUG|"2016-11-07T15:59:57.000Z"

02:28:12.31 (39316094)|USER_DEBUG|[3]|DEBUG|2016-11-07T10:59:57.000-05:00

| improve this answer | |

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.