[SalesForce] Date formatting issue in Apex

I'm trying to format the date into specific format in Apex class,
Desired result:
2015-05-19T18:52:30.937-07:00

By using SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); what I get is
2015-05-19T18:52:30.937-0700

I would like to know if the desired result (….-07:00 instead ….-0700) can be achieved without doing string manipulation, which is my last option.

Any help would be greatly appreciated.

Best Answer

you can achieve it by replacing the Z with XXX.

Datetime sdf = Datetime.now();

system.debug(sdf.format('yyyy-MM-dd\'T\'HH:mm:ss.SSSXXX'));

The result was:

2015-05-20T20:28:23.203+02:00

This is documented here