Salesforce Apex:ISO时间戳格式功能

Sal*_*ies 5 salesforce apex-code

我想知道是否有一个顶点日期或日期时间函数来格式化日期/时间到ISO格式的时间戳"2007-04-05T12:30-02:00"或者这是否必须通过其他方式创建?

谢谢.

Dan*_*ger 14

在Apex中,您可以使用DateTime format(string dataFormat)或format(string dataFormat, string timezone)方法.它接受一个dataFormat字符串,该字符串对应于Java简单日期格式.您需要为ISO 8601定义正确的格式.

另外,请考虑DateTime的时区.在下面的示例中,我使用formatGMT来避免时区偏移.

System.debug(datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\''));
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用JSON序列化程序.

System.debug(json.serialize(datetime.now()));
Run Code Online (Sandbox Code Playgroud)

  • 请注意`json.serialize()`:它似乎将返回的字符串包装在单引号中. (2认同)