2

Hi I am new to apex coding and am trying to parse a datetime and then later on in my program I am trying to convert it back into a string to query items.

For converting a string into a datetime I am using

String testDateString = '7/1/2015 1:30 PM';
DateTime testDate = DateTime.parse(sdate);

To convert the datetime back into a string I am using:

String convertedDate = testDate.formatGmt('yyyy-MM-dd\'T\'hh:mm:ss\'Z\'');

but this gives me the string 2015-07-01T08:30:00.000Z, which would be correct if the time was 7/1/2015 1:30 AM

but I am expecting the string

2015-07-01T20:30:00.000Z.

Is there a reason why the formatGmt ignores the AM/PM? Am i doing something clearly wrong?

Thanks for the help

2

You meant to use HH instead of hh. Lowercase hh returns only the 12-hour version of the hour, while using HH returns the zero-based 24-hour version of the hour. See Java's SimpleDateFormat for more details.

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.