I'm trying to format a DateTime to 24 hour time but it displays the wrong time between midnight and 1 AM.

String created = Datetime.now().formatGMT('yyyy-MM-dd\'T\'kk:mm:ss.SSS\'Z\'');

shows 2017-06-02T24:55:41.319Z for 12:55 AM instead of 2017-06-02T00:55:41.319Z. Is there a way to get it to display the correct time without modifying the string?

2 Answers 2


Take a look at SimpleDateFormat. If you want 0 based hours use H, not k.

H   Hour in day (0-23)
k   Hour in day (1-24)
K   Hour in am/pm (0-11)
h   Hour in am/pm (1-12)

Change k to h in string format:

String created = Datetime.now().formatGMT('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'');
  • Note that lower case h corresponds to upper case K in whether you want to display 12 or 24.
    – Adrian Larson
    Jun 6, 2017 at 14:22
  • @AdrianLarson yes, I've updated Jun 6, 2017 at 14:23
  • You missed one.
    – Adrian Larson
    Jun 6, 2017 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .